Or in the case of the Websocket_demo?
Rebar-creator Create-app Websocket_demo
Tree, look at the catalogue.
├──cert│├──cowboy-static│├──index.html│└──js│ └──jquery.min.js
Cert directory from the cowboy of the sample inside can be,CA certificate need all trust, browser Restart, specific google see
Static directory just get a little bit of it. Display the next HTTPS, this is used to test the integer there is no problem
Direct Sticker Code
Path_helper.erl
-Module (path_helper). -export ( [get_path/1]). Get_path (Extrapath), = file:get_cwd (), string: Concat (Currentpath,"/"), string: concat (Path,extrapath).
Route_helper.erl
-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.erl
-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, -, 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 ( +, 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 ( +, self (), <<"How ' Are 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.
Note that in SSL, the parameters of the Init
Online Test http://www.baiyangliu.com/lab/websocket/
Local WebSocket test Address
Wss://loclahost:8080/websocket
If you are prompted for SSL errors, it's best to take a look at the following, right?
Https://localhost:8080/static/index.html
Note: After SSL, access must be in domain name, not IP.
Example of WebSocket with SSL