我们的服务遍布中国

我们的服务遍布中国
乃至世界

光网所服务的品牌地域与城市
北京 天津 上海 广州 深圳 香港 厦门 江苏 浙江 山东
重庆 长沙 武汉 成都 西安 宁夏 丽江 青海 云南 乌鲁木齐
黑龙江 内蒙古 河北 ...
光网服务与合作的全球各地
美国 加拿大 德国 法国 英国 瑞士 意大利 荷兰
印度 日本 韩国 ...

不论你的品牌在何处
我们都可以提供完善的服务与帮助

致电

0512-56969630
您所在的位置:首页 > SSL证书

带ssl的websocket例子

发布时间:2015/1/1 16:31:52 浏览:1562打印字号:

还是在那个websocket_demo的例子

rebar-creator create-app websocket_demo

 

tree一下看看大概目录

copycode.gif
├── cert
│   ├── cowboy-ca.crt
│   ├── server.crt
│   └── server.key
├── src
│   ├── path_helper.erl
│   ├── route_helper.erl
│   ├── websocket_demo.app.src
│   ├── websocket_demo_app.erl
│   └── ws_handler.erl
├── static│   ├── index.html
│   └── js
│       └── jquery.min.js
copycode.gif

 

cert目录从cowboy的sample里面的拿过来即可,ca证书需要全部信任,浏览器得重启,具体google看看

static目录随便弄点过来显示下https即可,这个用来测试证书有没有问题的

 

直接贴代码

path_helper.erl

copycode.gif
-module(path_helper).-export([get_path/1]).

get_path(ExtraPath)->
    {ok,CurrentPath} = file:get_cwd(),
    Path = string:concat(CurrentPath,"/"),    string:concat(Path,ExtraPath).
copycode.gif

 

route_helper.erl

copycode.gif
-module(route_helper).-export([get_routes/0]).

get_routes() ->
    StaticPath = path_helper:get_path("../static/"),
    [
        {'_', [
            {"/websocket", ws_handler, []},
            {"/static/[...]", cowboy_static, {dir, StaticPath}}
        ]}
    ].
copycode.gif

 

websocket_demo_app.erl

copycode.gif
-module(websocket_demo_app).-behaviour(application).-export([start/2, stop/1]).

start(_Type, _Args) ->

    ok = application:start(crypto),
    ok = application:start(cowlib),
    ok = application:start(ranch),
    ok = application:start(cowboy),

    CertDir = path_helper:get_path("../cert/"),
    io:format("~p~n",[CertDir]),

    Routes    = route_helper:get_routes(),
    Dispatch  = cowboy_router:compile(Routes),
    Port      = 8080,
    TransOpts = [
        {port, Port},
        {cacertfile, CertDir ++ "/cowboy-ca.crt"},
        {certfile, CertDir ++ "/server.crt"},
        {keyfile, CertDir ++ "/server.key"}
    ],
    ProtoOpts = [{env, [{dispatch, Dispatch}]}],


    {ok, _} = cowboy:start_https(https,100, TransOpts, ProtoOpts).

stop(_State) ->
    ok.
copycode.gif

 

ws_handler.erl

复制代码
-module(ws_handler).-behaviour(cowboy_websocket_handler).-export([init/3]).-export([websocket_init/3]).-export([websocket_handle/3]).-export([websocket_info/3]).-export([websocket_terminate/3]).

init({tcp, http}, _Req, _Opts) ->
    io:format("init ~n"),
    {upgrade, protocol, cowboy_websocket};
init({ssl, http}, _Req, _Opts) ->
    io:format("ssl init ~n"),
    {upgrade, protocol, cowboy_websocket}.

websocket_init(_TransportName, Req, _Opts) ->
    io:format("websocket_init ~n"),
    erlang:start_timer(1000, self(), <<"Hello!">>),
    {ok, Req, undefined_state}.

websocket_handle({text, Msg}, Req, State) ->
%%     io:format("websocket_handle text ~p,~p,~p~n",[Msg,Req,State]),
    {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
websocket_handle(_Data, Req, State) ->
%%     io:format("websocket_handle ~p,~p,~p~n",[_Data,Req,State]),
    {ok, Req, State}.

websocket_info({timeout, _Ref, Msg}, Req, State) ->
    %io:format("websocket timeout ~n"),
    erlang:start_timer(1000, self(), <<"How' you doin'?">>),
    {reply, {text, Msg}, Req, State};
websocket_info(_Info, Req, State) ->
    io:format("websocket_info ~p,~p,~p~n",[_Info,Req,State]),
    {ok, Req, State}.

websocket_terminate(_Reason, _Req, _State) ->
    io:format("terminate ~n"),
    ok.
copycode.gif

注意,在ssl的是,init的参数

 

在线测试http://www.baiyangliu.com/lab/websocket/

本地websocket测试地址

wss://loclahost:8080/websocket

如果提示ssl错误什么的,最好先看看下面这个对不对

https://localhost:8080/static/index.html

 

注:ssl以后,访问必须以域名,不能ip。