This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathhybridsocket.js
More file actions
140 lines (121 loc) · 2.78 KB
/
Copy pathhybridsocket.js
File metadata and controls
140 lines (121 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var handler = require('./common/handler');
var protocol = require('pomelo-protocol');
var logger = require('pomelo-logger').getLogger('pomelo', __filename);
var Package = protocol.Package;
var ST_INITED = 0;
var ST_WAIT_ACK = 1;
var ST_WORKING = 2;
var ST_CLOSED = 3;
/**
* Socket class that wraps socket and websocket to provide unified interface for up level.
*/
var Socket = function(id, socket) {
EventEmitter.call(this);
this.id = id;
this.socket = socket;
if(!socket._socket) {
this.remoteAddress = {
ip: socket.address().address,
port: socket.address().port
};
} else {
this.remoteAddress = {
ip: socket._socket.remoteAddress,
port: socket._socket.remotePort
};
}
var self = this;
socket.once('close', this.emit.bind(this, 'disconnect'));
socket.on('error', this.emit.bind(this, 'error'));
socket.on('message', function(msg) {
if(msg) {
msg = Package.decode(msg);
handler(self, msg);
}
});
this.state = ST_INITED;
// TODO: any other events?
};
util.inherits(Socket, EventEmitter);
module.exports = Socket;
/**
* Send raw byte data.
*
* @api private
*/
Socket.prototype.sendRaw = function(msg) {
if(this.state !== ST_WORKING) {
return;
}
var self = this;
this.socket.send(msg, {binary: true}, function(err) {
if(!!err) {
logger.error('websocket send binary data failed: %j', err.stack);
return;
}
});
};
/**
* Send byte data package to client.
*
* @param {Buffer} msg byte data
*/
Socket.prototype.send = function(msg) {
if(msg instanceof String) {
msg = new Buffer(msg);
} else if(!(msg instanceof Buffer)) {
msg = new Buffer(JSON.stringify(msg));
}
this.sendRaw(Package.encode(Package.TYPE_DATA, msg));
};
/**
* Send byte data packages to client in batch.
*
* @param {Buffer} msgs byte data
*/
Socket.prototype.sendBatch = function(msgs) {
var rs = [];
for(var i=0; i<msgs.length; i++) {
var src = Package.encode(Package.TYPE_DATA, msgs[i]);
rs.push(src);
}
this.sendRaw(Buffer.concat(rs));
};
/**
* Send message to client no matter whether handshake.
*
* @api private
*/
Socket.prototype.sendForce = function(msg) {
if(this.state === ST_CLOSED) {
return;
}
this.socket.send(msg, {binary: true});
};
/**
* Response handshake request
*
* @api private
*/
Socket.prototype.handshakeResponse = function(resp) {
if(this.state !== ST_INITED) {
return;
}
this.socket.send(resp, {binary: true});
this.state = ST_WAIT_ACK;
};
/**
* Close the connection.
*
* @api private
*/
Socket.prototype.disconnect = function() {
if(this.state === ST_CLOSED) {
return;
}
this.state = ST_CLOSED;
this.socket.emit('close');
this.socket.close();
};