帶ssl的websocket例子

仍是在那個websocket_demo的例子html

rebar-creator create-app websocket_demo

 

tree一下看看大概目錄jquery

├── 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

 

cert目錄從cowboy的sample裏面的拿過來便可,ca證書須要所有信任,瀏覽器得重啓,具體google看看web

static目錄隨便弄點過來顯示下https便可,這個用來測試證書有沒有問題的瀏覽器

 

直接貼代碼websocket

path_helper.erlapp

-module(path_helper).

-export([get_path/1]).

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

 

route_helper.erlsocket

-module(route_helper).

-export([get_routes/0]).

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

 

websocket_demo_app.erltcp

-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.

 

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.

注意,在ssl的是,init的參數google

 

在線測試http://www.baiyangliu.com/lab/websocket/

本地websocket測試地址

wss://loclahost:8080/websocket

若是提示ssl錯誤什麼的,最好先看看下面這個對不對

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

 

注:ssl之後,訪問必須以域名,不能ip。

相關文章
相關標籤/搜索