-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsreceiver.js
42 lines (34 loc) · 1.1 KB
/
wsreceiver.js
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
var util = require('util');
var stream = require('stream');
// WebSocketからの指示を受け取るWritable Stream
function WebSocketReceiver(){
this.writable = true;
this.readable = true;
this.onmessage = function(data){};
this.piped = false;
}
util.inherits(WebSocketReceiver,stream.Stream);
WebSocketReceiver.prototype.send = function(data){
this.emit('data',data);
}
WebSocketReceiver.prototype.end = function(data){
if(data) this.write(data);
this.writable = false;
}
WebSocketReceiver.prototype.write = function(data){
var data = data.toString().trim();
this.onmessage(JSON.parse(data));
return true;
}
WebSocketReceiver.prototype.resume = function(){};
WebSocketReceiver.prototype.pause = function(){};
WebSocketReceiver.prototype.pipe = function(dest){
this.piped = true;
this.on('data',function(data){
dest.write(data);
});
}
WebSocketReceiver.prototype.setEncoding = function(encoding){};
WebSocketReceiver.prototype.destroy = function() {};
WebSocketReceiver.prototype.destroySoon = function() {};
module.exports = WebSocketReceiver;