const dgram = require('dgram');
let fs = require('fs');
exports = module.exports = CjChannelUdp;
function CjChannelUdp() {
this.connectState = CjChannelUdp.CI_ConnectState_Null;
this._udpSocket = null;
this.isAutoOpen = false;
this.isAutoHeartbeat = false;
this.connectParams = {LocalPort: 5555, LocalIpAddress: '127.0.0.1', RemotePort: 5556, RemoteIpAddress: '127.0.0.1'};
this.onReceived = null;
}
CjChannelUdp.prototype.receivedData = function(data, rinfo) {
console.log('received data: ', data.length);
if (this.onReceived) this.onReceived(data);
// var data = Buffer.from('hello')
// this._udpSocket.send([data], rinfo.port, rinfo.address, (err) => {
// // client.close();
// }
// )
};
CjChannelUdp.prototype.sendData = function(data) {
if (this.isOpen()) {
if (Number.isNaN(parseInt(this.connectParams.RemotePort))) {
return 0;
}
this._udpSocket.send(data, this.connectParams.RemotePort, this.connectParams.RemoteIpAddress, (err) => {
if (err !== null) {
console.log(err);
}
});
}
return 0;
};
CjChannelUdp.CI_ConnectState_Null = 0;
CjChannelUdp.CI_ConnectState_Disconnected = 1;
CjChannelUdp.CI_ConnectState_Connecting = 2;
CjChannelUdp.CI_ConnectState_ConnectTimeout = 3;
CjChannelUdp.CI_ConnectState_Connected = 4;
CjChannelUdp.CS_EntryRemoteIpAddress = 'RemoteIpAddress';
CjChannelUdp.CS_EntryRemotePort = 'RemotePort';
CjChannelUdp.CS_EntryLocalIpAddress = 'LocalIpAddress';
CjChannelUdp.CS_EntryLocalPort = 'LocalPort';
/**
* @param option = {LocalIpAddress:'127.0.0.1', LocalPort: 5555, RemoteIpAddress: '127.0.0,.1', RemotePort: 5556};
*/
CjChannelUdp.prototype.open = function(option) {
// var option = {port:5555, ip:'127.0.0.1'};
if (this._udpSocket) {
return;
}
if (this.connectState === CjChannelUdp.CI_ConnectState_Connecting) {
return;
}
let self = this;
if (option) self.connectParams = option;
if (Number.isNaN(parseInt(self.connectParams.LocalPort))) {
console.log('open fail. connectParams.LocalPort is invalid.');
return;
}
if (Number.isNaN(parseInt(self.connectParams.RemotePort))) {
console.log('connectParams.RemotePort is invalid.');
}
self.connectState = CjChannelUdp.CI_ConnectState_Connecting;
let udpSocket = null;
let connectTimeout = function() {
self.connectState = CjChannelUdp.CI_ConnectState_ConnectTimeout;
self._udpSocket = null;
if (udpSocket) {
udpSocket.close();
}
console.log('connect timeout.');
};
let timeout = setTimeout(connectTimeout, 5 * 1000);
udpSocket = dgram.createSocket('udp4');
udpSocket.on('error', function(err) {
self._udpSocket = null;
self.connectState = CjChannelUdp.CI_ConnectState_Disconnected;
self.close();
});
udpSocket.on('message', function(msg, rinfo) {
self.receivedData.call(self, msg, rinfo);
});
udpSocket.on('listening', function() {
if (self._udpSocket) {
console.log('had _udp, system error, or connect timeout.');
return;
}
clearTimeout(timeout);
// 'connect' listener
console.log('connected to server!');
self._udpSocket = udpSocket;
self.connectState = CjChannelUdp.CI_ConnectState_Connected;
});
// if (self.connectParams.LocalIpAddress)
// udpSocket.bind(self.connectParams.LocalPort, self.connectParams.LocalIpAddress);
// else
udpSocket.bind(self.connectParams.LocalPort);
self.checkChannel(3000);
};
CjChannelUdp.prototype.close = function() {
if (this._udpSocket) {
this._udpSocket = null;
this._udpSocket.close();
}
};
CjChannelUdp.prototype.isOpen = function() {
return this._udpSocket && this.connectState == CjChannelUdp.CI_ConnectState_Connected;
};
CjChannelUdp.prototype.checkChannel = function(interval) {
let udp = this;
if (interval < 1000) {
if (udp.checkChannelTimer) {
clearTimeout(udp.checkChannelTimer);
}
return;
}
if (udp.checkChannelTimer) {
clearTimeout(udp.checkChannelTimer);
}
var timeOut = function() {
//* recycle connect
if (udp.isAutoOpen) {
if (!udp.isOpen()) {
udp.open();
console.log('timer auto open');
}
}
//* recycle heart jump
if (udp.isAutoHeartbeat) {
if (udp.isOpen()) {
udp.send('heart jump!\r\n');
console.log('timer auto heart jump!');
}
}
udp.checkChannelTimer = setTimeout(timeOut, interval);
};
udp.checkChannelTimer = setTimeout(timeOut, interval);
};
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器,这个客户端还活着.事实上这是为了保持长连接,至于这个包的内容,是没有什么特别规定的,不过一般都是很小的包,或者只包含包头的一个空包. 在TCP的机制里面,本身是存在有心跳包的机制的,也就是TCP的选项:SO_KEEPALIVE.系统默认是设置的2小时的心跳频率.但是它检查不到机器断电.网线拔出.防火墙这些断线.而且逻辑层处理断线可能也不是那么好处理.一般,如果只是用于保活还是可以的. 心跳包一般来说都…
资源推荐
资源详情
资源评论
收起资源包目录
cjchannel_udp.rar (1个子文件)
cjchannel_udp.js 5KB
共 1 条
- 1
资源评论
oudream
- 粉丝: 7
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功