创建工程

rebar-creator create-app websocket_demo

文件列表

route_helper.erl

-module(route_helper).

-export([get_routes/]).

get_routes() ->
[
{'_', [
{"/websocket", ws_handler, []}
]}
].

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), Routes = route_helper:get_routes(),
Dispatch = cowboy_router:compile(Routes),
Port = ,
TransOpts = [{port, Port}],
ProtoOpts = [{env, [{dispatch, Dispatch}]}],
cowboy:start_http(http, , 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}. 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.

在线测试http://www.baiyangliu.com/lab/websocket/

本地websocket测试地址

ws://127.0.0.1:8080/websocket

文本消息和binary消息自己打印慢慢看即可

erlang的websocket例子的更多相关文章

  1. Erlang cowboy websocket 服务器

    Erlang cowboy websocket 服务器 原文见于: http://marcelog.github.io/articles/erlang_websocket_server_cowboy_ ...

  2. 带ssl的websocket例子

    还是在那个websocket_demo的例子 rebar-creator create-app websocket_demo tree一下看看大概目录 ├── cert │   ├── cowboy- ...

  3. golang gorilla websocket例子

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

  4. (网页)websocket例子

    转载自博客园张果package action; import javax.websocket.CloseReason; import javax.websocket.OnClose; import j ...

  5. 基于netty的websocket例子

    nettyServer package com.atguigu.netty.websocket; import javax.annotation.PostConstruct; import org.s ...

  6. Springboot WebSocket例子

    Springboot整合WebSocket 1.application.properties #设置服务端口号 server.port=8080 #thymeleaf配置 #是否启用模板缓存. spr ...

  7. WebSocket实战之——JavaScript例子

    一.详细代码案例 详细解读一个简单html5 WebSocket的Js实例教程,附带完整的javascript websocket实例源码,以及实例代码效果演示页面,并对本实例的核心代码进行了深入解读 ...

  8. html websocket

    from:http://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/ websocket 规范升级过,在该链接的文章内未提及,后面 ...

  9. erlang 200w进程测试

    参照<programing erlang>书例子,测试erlang创建进程性能 创建N个进程 都wait,然后挨个发送die关闭进程,代码如下: 测试场景:200w进程.MacBook P ...

随机推荐

  1. python 语法错误记录

    1  Missing parameter end_time in docstring less... (Ctrl+F1) 参数位置错误 注意:只有在形参表末尾的那些参数可以有默认参数值,即你不能在声明 ...

  2. spring中aop使用

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查.缓存) Spring的基 ...

  3. mysqldump 使用方法

    1.仅导出数据: mysqldump -t -uroot -proot pgenius RES_COM_PFT_FCST>RES_COM_PFT_FCST.sql 2.有条的导出数据: mysq ...

  4. zoj3656

    题解: 按照位展开,然后一位一位判断 注意判断给出数据是否有问题 代码: #include<cstdio> #include<cmath> #include<algori ...

  5. 关于px em rem的一点小总结

    2015-11-28 06:06:40 概念 都是CSS单位. px:像素 Pixel.像素 (计算机屏幕上的一个点) em:1em 等于当前的字体尺寸. 2em 等于当前字体尺寸的两倍. 例如,如果 ...

  6. OC-初识面向对象

    面向对象和面向过程思想 OC是面向对象的,C是面向过程的.面向对象和面向过程只是解决问题的两种不同思想 面向对象和面向过程的区别 以用电脑听歌为例子 面向过程 打开电脑 播放电脑中的歌曲 关闭电脑 面 ...

  7. 自己定义的Excetpion继承哪个异常有什么讲究?[待解答]

    try catch的地方需要用到一个自定义的DBException,如下: 于是我就自定义了一个DBException,继承Excetpion,以父类Exception构造器创建构造器: DBExce ...

  8. Hibernate报错:Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value

    问题一: 报错如下: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assig ...

  9. Linux基本概念及操作

    基本概念及操作 一.实验介绍 1.1 实验内容: 实验楼环境介绍 常用 Shell 命令及快捷键 Linux 使用小技巧 1.2 实验知识点 Linux 基本命令 通配符的使用 基本软件安装 查看帮助 ...

  10. IOS开发 基础框架(Fondation Framework)的线程安全

    有一种误解,认为基础框架(Foundation framework)是线程安全的,而Application Kit是非线程安全的.不幸的是,这是一个总的概括,从而造成一点误导.每个框架都包含了线程安全 ...