创建工程

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. Post with HttpClient4

    转载:http://www.cnblogs.com/luxiaoxun/p/6165237.html 作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ HttpC ...

  2. laravel中上传图片之后图片的处理

    $file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...

  3. 八、dbms_rls(实现精细访问控制)

    1.概述 本报只适用于Oracle Enterprise Edition,它用于实现精细访问控制,并且精细访问控制是通过在SQL语句中动态增加谓词(WHERE子句)来实现的.通过使用ORACLE的精细 ...

  4. Linq的使用 <一>

    一.种类 1.Linq to Objects,实现了IEnumerable<T>集合对象的集成查询 2.Linq to sql,针对关系数据库MSSQL的解释查询 3.Linq to En ...

  5. log4cpp

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. fegin--调用超时设置

    Feign请求超时问题 Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码.而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间 ...

  7. angular2.0学习日记1

    使用NG2之前需要安装node以及Npm环境,并到node下下载ng2所需要得文件,具体配置请到https://angular.cn/docs/ts/latest/quickstart.html按照提 ...

  8. angular 与jQuery混用 大坑一

    由于angular是在真实dom加载之后,运行,所以jQuery操作dom时,要先于angular添加的元素(如,ng-repeat),所以常常会出现,元素错位,解决办法:添加一个透明的站位元素:

  9. HDU 数位dp

    模板http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html 完全理解以后,我发现这种写法实在是太厉害了,简洁,优美,可以回避很多细节 ...

  10. 提问:错误提示--命名空间“System.Web”中不存在类型或命名空间名称“UI”(是缺少程序集引用吗?) 如何解决??

    转自:http://topic.csdn.net/u/20080723/21/7e1e06cb-d013-4675-ba9b-9b39d49d0a0e.html 1) 添加对System.Web.dl ...