带ssl的websocket例子

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

还是在那个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。