还是在那个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例子的更多相关文章

  1. 用nodejs快速实现websocket服务端(带SSL证书生成)

    有不少公司将nodejs的socket.io作为websocket的解决方案,很遗憾的是socket.io是对websocket的封装,并不支持html5原始的websocket协议,微信小程序使用的 ...

  2. golang gorilla websocket例子

    WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信--允许服务器主动发送信息给客户端. WebSocket通信协议于2011年被IETF定 ...

  3. PHP 通过带SSL的SMTP 发送邮件的处理

    客户端与SMTP服务器的通讯, 是通过固定的命令以及返回编号完成的. 发送Email, 需要经过的步骤有创建socket (区分带ssl, 还是不带ssl)执行命令, 并检查返回值是否与预期一致, 不 ...

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

  5. Web通信协议:OSI、TCP、UDP、Socket、HTTP、HTTPS、TLS、SSL、WebSocket、Stomp

    1      各层的位置 1.1      OSI七层模型全景图 OSI是Open System Interconnect的缩写,意为开放式系统互联. 1.2      五层网络协议 在七层的基础上, ...

  6. Spring Boot SSL [https]配置例子

    前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...

  7. 带你走近WebSocket协议

    一.WebSocket协议是什么? WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. 二.那为什么我们要用WebSocket协议呢? 了解计算机网络协议的 ...

  8. 带SSL证书的httpclient 远程接口工具类

    package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...

  9. 在Mac上搭建带ssl协议和域名指向的Apache服务器

    顾名思义,就是要在苹果电脑上搭建 Apache 服务器,并且支持 https 协议,能用指定域名访问(有些开发调试需要注册域名,比如调试微信JS-SDK),当然最好能在手机端进行调试.首先,Mac 系 ...

随机推荐

  1. Python中实现switchcase

    # 第一种方式使用python中的字典# author:wanstack def first_func(): print('first_func') def second_func(): print( ...

  2. VS展开当前目录

  3. linux中安装eclipse--CnetOS6.5

    01.去官网下载指定的eclipse安装包 02.使用xftp把下载的eclipse安装包放入到linux系统的指定位置03.到指定的目录下!使用命令解压下载的文件tar -zxvf 文件名称04. ...

  4. LeetCode OJ:Bulls and Cows (公牛与母牛)

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  5. SCRF的简介及防护手段

    CSRF全拼为Cross Site Request Forgery,译为跨站请求伪造. CSRF指攻击者盗用了你的身份,以你的名义发送恶意请求. 包括:以你名义发送邮件,发消息,盗取你的账号,甚至于购 ...

  6. APUE学习笔记——5.9Binary I/O 二进制读写

    概述:         二进制I/O,通常用来一次性读写结构化的数据块.(因为有时候我们要读写的数据中包含换行符或者Null字符,无法使用fgets或fputs,而使用getc和putc又需要花费太多 ...

  7. zookeeper数据一致性与paxos算法

    数据一致性与paxos算法 据说Paxos算法的难理解与算法的知名度一样令人敬仰,所以我们先看如何保持数据的一致性,这里有个原则就是: 在一个分布式数据库系统中,如果各节点的初始状态一致,每个节点都执 ...

  8. [置顶] 滴滴插件化VirtualAPK框架原理解析(二)之Service 管理

    在前一篇博客滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理 中VirtualAPK是如何对Activity进行管理的,本篇博客,我们继续来学习这个框架,这次我们学习的是如何 ...

  9. jmeter返回报文乱码问题

    返回的报文中存在乱码如下: 1.先改脚本里面的 content encoding为utf-8 然后response为utf-8 如果以上还是不可以,那就改配置文件jmeter.properties,里 ...

  10. 程序设计入门-C语言基础知识-翁恺-第四周:循环控制-详细笔记(四)

    目录 第四周:循环控制 4-1 for循环 4-2 循环控制 各运算符优先级(图) 4-3 课后习题 4-4 讨论题 第四周:循环控制 4-1 for循环 for循环像一个计数循环:设定一个计数器,初 ...