还是在那个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. 一道经典的面试题:如何从N个数中选出最大(小)的n个数

    转载:https://zhidao.baidu.com/question/1893908497885440140.html 这个问题我前前后后考虑了有快一年了,也和不少人讨论过.据我得到的消息,Goo ...

  2. 转载:【Oracle 集群】RAC知识图文详细教程(七)--Oracle 11G RAC集群安装

    文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...

  3. SpringBoot全家桶

    前言 Spring简化了Java的开发,而SpringBoot简化了Spring.本文用SpringBoot采用分层的结构整合了filter,aspect,mybaits,logback,redis, ...

  4. LeetCode OJ:Number of 1 Bits(比特1的位数)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. C# Http方式下载文件到本地类

    直接上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  6. PostgreSQL数据库资料(转)

    PostgreSQL数据库资料 转自:http://blog.csdn.net/postgrechina/article/details/49132791 推荐书籍: 概念书籍: <Postgr ...

  7. Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结

    知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式.英语叫做IPv4 numbers-and-dots notation. 如果把210.25.132.181转换 ...

  8. 深度学习(七十)darknet 实现编写mobilenet源码

    一.添加一个新的网络层 (1)parse.c文件中函数string_to_layer_type,添加网络层类型解析: if (strcmp(type, "[depthwise_convolu ...

  9. Swift 保存pdf并在webview显示

    本例中服务器返回的是base64 字符串,也可以直接返回byte[],看server接口的定义 let nsData = NSData(base64Encoded: data!!) 拿到nsData以 ...

  10. crm 03--->销售页面及逻辑

    基本思路 销售  ------->使用的是customer表来操作 有两个页面: 未成交的客户 公共客户页面  ---  什么属于?  --> 三天未跟进,及十五天内未成交 url    ...