erlang的websocket例子
创建工程
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例子的更多相关文章
- Erlang cowboy websocket 服务器
Erlang cowboy websocket 服务器 原文见于: http://marcelog.github.io/articles/erlang_websocket_server_cowboy_ ...
- 带ssl的websocket例子
还是在那个websocket_demo的例子 rebar-creator create-app websocket_demo tree一下看看大概目录 ├── cert │ ├── cowboy- ...
- golang gorilla websocket例子
WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信--允许服务器主动发送信息给客户端. WebSocket通信协议于2011年被IETF定 ...
- (网页)websocket例子
转载自博客园张果package action; import javax.websocket.CloseReason; import javax.websocket.OnClose; import j ...
- 基于netty的websocket例子
nettyServer package com.atguigu.netty.websocket; import javax.annotation.PostConstruct; import org.s ...
- Springboot WebSocket例子
Springboot整合WebSocket 1.application.properties #设置服务端口号 server.port=8080 #thymeleaf配置 #是否启用模板缓存. spr ...
- WebSocket实战之——JavaScript例子
一.详细代码案例 详细解读一个简单html5 WebSocket的Js实例教程,附带完整的javascript websocket实例源码,以及实例代码效果演示页面,并对本实例的核心代码进行了深入解读 ...
- html websocket
from:http://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/ websocket 规范升级过,在该链接的文章内未提及,后面 ...
- erlang 200w进程测试
参照<programing erlang>书例子,测试erlang创建进程性能 创建N个进程 都wait,然后挨个发送die关闭进程,代码如下: 测试场景:200w进程.MacBook P ...
随机推荐
- Java内存状况查看方法和分析工具
Java本身提供了多种丰富的方法和工具来帮助开发人员查看和分析GC及其JVM内存的状况,同时开源界也有一些工具用于查看和分析GC和JVM内存的状况. 通过这些分析,可以排查程序中内存泄露的问题及调优程 ...
- Arison [JS]window.location获取url各项参数详解
https://www.cnblogs.com/Arison/p/5286368.html 对于这样一个URL代码如下 复制代码 http://www.php230.com :80/fisker/po ...
- Java--------------Windows下Redis的安装使用
摘要 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted s ...
- ZOJ 3696 Alien's Organ(泊松定理,期望值)
Alien's Organ Time Limit: 2 Seconds Memory Limit: 65536 KB There's an alien whose name is Marja ...
- Server.Transfer 页面之间传值
server.transfer 特点: 1:大家熟悉的一个特点,用server.transfer 跳转到新页面时,浏览器的地址是没有改变的(因为重定向完全在服务器端进行,浏览器根本不知道服务器已经执行 ...
- KindEditor富文本编辑框和BeautifulSoup的基本使用
KindEditor富文本编辑框 1.进入官网 2.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files ...
- C++11_ Lambda
版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的Lambda语法,一个非常给力的语法 1.组成 : [...导入符号](...参数)mutable(可改写) throw ...
- kali linux下不能以root权限运行vlc的解决办法
习惯了在Linux下面使用VLC播放视频和音乐, 但是 VLC 的 linux 版本并不支持在root下面运行. 终端运行vlc命令报错,错误信息如下 root@kbdancer:~# vlc VLC ...
- shell脚本实例一
一. 什么是shell 脚本时一种解释性语言: shell脚本保存执行动作: 脚本判定命令的执行条件 脚本来实现动作的批量执行.二.如何创建 vim test.sh ##shell脚本一般都 ...
- ffmpeg命令汇总
1. 查看ffmpeg信息: ldd `which ffmpeg` ffmpeg -filters ffmpeg -h filter=drawtext man ffmpeg ffmpeg --help ...