clouds-socket
安装
$ npm install clouds-socket --save
使用
服务端(TCP/Unix domain)
var socket = ; // 创建服务端实例var server = socket; // 当有新客户端连接时,触发connection事件server; // 当发生错误时触发error事件server; // 当退出服务端时触发exit事件server; // 关闭服务器server;
客户端(TCP/Unix domain)
var socket = ; // 创建客户端实例var client = socket; // 当连接成功时触发connect事件// 如果服务器端断开了连接,客户端会自动重连// 再次重连成功还是会触发connect事件client; // 测试网络延时client; // 当收到数据时,触发data事件client; // 当发生错误时触发error事件client; // 当退出服务端时触发exit事件client; // 关闭连接client;
数据报文(UDP):
var socket = ; // 创建实例var datagram = socket; // 监听端口datagram; // 发送数据datagram; // 测试网络延时datagram; // 当收到数据时,触发data事件datagram; // 当发生错误时触发error事件datagram; // 当退出服务端时触发exit事件datagram; // 关闭连接datagram;
通过环境变量配置默认的值
# 设置单条UDP消息的最大长度,默认576 export CLOUDS_DEFAULT_UDP_MESSAGE_SIZE=8192 # 设置TCP连接被断开后,自动重连的等待时间,ms,默认500 export CLOUDS_DEFAULT_RECONNECT_WAITING=200
测试代码覆盖率
89% coverage 563 SLOC
授权协议
Copyright (c) 2012-2015 Zongmin Lei (雷宗民) <leizongmin@gmail.com>
http://ucdok.com
The MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.