带ssl的websocket例子
还是在那个websocket_demo的例子
rebar-creator create-app websocket_demo
tree一下看看大概目录
├── 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看看
static目录随便弄点过来显示下https即可,这个用来测试证书有没有问题的
直接贴代码
path_helper.erl
-module(path_helper). -export([get_path/]). get_path(ExtraPath)->
{ok,CurrentPath} = file:get_cwd(),
Path = string:concat(CurrentPath,"/"),
string:concat(Path,ExtraPath).
route_helper.erl
-module(route_helper). -export([get_routes/]). 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/, stop/]).
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 = ,
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/]).
-export([websocket_init/]).
-export([websocket_handle/]).
-export([websocket_info/]).
-export([websocket_terminate/]). 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' 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的参数
在线测试http://www.baiyangliu.com/lab/websocket/
本地websocket测试地址
wss://loclahost:8080/websocket
如果提示ssl错误什么的,最好先看看下面这个对不对
https://localhost:8080/static/index.html
注:ssl以后,访问必须以域名,不能ip。
带ssl的websocket例子的更多相关文章
- 用nodejs快速实现websocket服务端(带SSL证书生成)
有不少公司将nodejs的socket.io作为websocket的解决方案,很遗憾的是socket.io是对websocket的封装,并不支持html5原始的websocket协议,微信小程序使用的 ...
- golang gorilla websocket例子
WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信--允许服务器主动发送信息给客户端. WebSocket通信协议于2011年被IETF定 ...
- PHP 通过带SSL的SMTP 发送邮件的处理
客户端与SMTP服务器的通讯, 是通过固定的命令以及返回编号完成的. 发送Email, 需要经过的步骤有创建socket (区分带ssl, 还是不带ssl)执行命令, 并检查返回值是否与预期一致, 不 ...
- Qt官方对OpenSSL的编译方法的描述,单独下载的Qt library则一般不带SSL(包括QT FAQ)
https://wiki.qt.io/MSYS2http://wiki.qt.io/Compiling_OpenSSL_with_MinGWhttps://wiki.qt.io/MinGW-64-bi ...
- Web通信协议:OSI、TCP、UDP、Socket、HTTP、HTTPS、TLS、SSL、WebSocket、Stomp
1 各层的位置 1.1 OSI七层模型全景图 OSI是Open System Interconnect的缩写,意为开放式系统互联. 1.2 五层网络协议 在七层的基础上, ...
- Spring Boot SSL [https]配置例子
前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...
- 带你走近WebSocket协议
一.WebSocket协议是什么? WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. 二.那为什么我们要用WebSocket协议呢? 了解计算机网络协议的 ...
- 带SSL证书的httpclient 远程接口工具类
package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...
- 在Mac上搭建带ssl协议和域名指向的Apache服务器
顾名思义,就是要在苹果电脑上搭建 Apache 服务器,并且支持 https 协议,能用指定域名访问(有些开发调试需要注册域名,比如调试微信JS-SDK),当然最好能在手机端进行调试.首先,Mac 系 ...
随机推荐
- Web框架之Django-20-基于mysql数据库的连接
Web框架之Django-20-基于mysql数据库的连接 想要连接mysql首先需要安装pymysql这个驱动 然后在app的init文件中引入驱动 import pymysql pym ...
- URAL 1557 Network Attack 图论,连通性,tarjain,dfs建树,分类讨论 难度:2
http://acm.timus.ru/problem.aspx?space=1&num=1557 1557. Network Attack Time limit: 2.0 secondMem ...
- 转载:【Oracle 集群】RAC知识图文详细教程(三)--RAC工作原理和相关组件
文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...
- String.Remove
String.Remove方法注意事项: 1.该方法不改变元字符串: 2.str = ‘’: str.Remove(str.Length-3);明显超限,但是不报错,返回值为''; str = '1 ...
- elasticsearch关于索引切分的实现
[背景信息] ES一直以来对于已经创建好的索引的分片是不可以进行分割的,简单的说,当你创建了一个索引,并指定了number_of_shards为2,当随着数据量的不断增大,是无法将索引的shard扩充 ...
- Linux输入输出管理
一.系统输入输出的理解 运行一个程序时,需要从某个位置读取输入信息,然后CPU处理,最后将输出 显示在屏幕或文件中:其中,某个位置相当于输入设备,屏幕或文件为输出设备. 标准输入:stdin,默认 ...
- Linux:history命令详解
Linux下History命令 主要用于显示历史指令记录内容, 下达历史纪录中的指令 . 语法 history [n] history [-c] history [-raw] histfiles ...
- win10 安装MongoDB
win10上面安装mongodb的时候,注意不要勾选上Install MongoDB Compass, 否则会退出报错!!!! mongodb的安装 我是在E盘建立的一个mongodb文件夹,用来安装 ...
- 基于视觉的 SLAM/Visual Odometry (VO) 开源资料、博客和论文列表
基于视觉的 SLAM/Visual Odometry (VO) 开源资料.博客和论文列表 以下为机器翻译,具体参考原文: https://github.com/tzutalin/awesome-vis ...
- 深度学习(六十四)Faster R-CNN物体检测