[英]How to write a websocket client
我正在嘗試在node.js上對我的websocket進行單元測試,並且想要模擬一個websocket客戶端。 我可以創建一個只連接到我的服務器的HTML文件,但是我無法在服務器上運行單個測試。
我將如何(使用http.Client
或net.Stream
)創建一個websocket客戶端並讓它與我的服務器進行交互。
我正在針對websocket規范的76(即將死)草案76。
我正在使用的服務器端實現是這個
既然你已經知道所有當前的WebSocket版本很快就會過時而且你正在使用一個支持舊75草案的WebSocket服務器,如果你已經有一些服務器代碼,那么制作一個很簡單,所以不需要76年的“安全”標題。
免責聲明:這件事只有5分鍾的測試時間,但它應該在大多數情況下都有效。
var net = require('net');
function WebSocket(host, port, encoder, decoder) {
this.encoder = encoder || function(data){return data.toString()};
this.decoder = decoder || function(data){return data};
this.socket = net.createConnection(port, host);
this.connected = false;
this.header = 0;
this.bytesSend = 0;
this.dataFrames = [];
this.dataState = 0;
var that = this;
process.nextTick(function() {
that.init(host, port);
});
}
// Prototype -------------------------------------------------------------------
WebSocket.prototype = {
onConnect: function() {console.log('connect');},
onClose: function() {console.log('close');},
onData: function(data) {console.log(data)},
init: function(host, port) {
var that = this;
this.socket.addListener('connect', function() {
var data ='GET / HTTP/1.1\r\n'
+ 'Host: ' + host + ':' + port + '\r\n'
+ 'Origin: websocket.node.js\r\n'
+ 'Connection: Upgrade\r\n'
+ 'Upgrade: WebSocket\r\n\r\n';
that.socket.write(data, 'ascii');
that.socket.flush();
});
this.socket.addListener('data', function(data) {that.read(data);});
this.socket.addListener('end', function() {that.onClose();});
this.socket.addListener('error', function(e) {console.log(e.message);that.close();});
},
send: function(data, encoded) {
if (this.connected) {
return this.write(encoded ? data : this.encoder(data));
} else {
return 0;
}
},
close: function() {
if (this.connected) {
this.connected = false;
this.write(null);
this.socket.end();
this.socket.destroy();
}
},
read: function read(data) {
for(var i = 0, l = data.length; i < l; i++) {
var b = data[i];
if (this.header < 4) {
if ((this.header === 0 || this.header === 2) && b === 0x0d) {
this.header++;
} else if ((this.header === 1 || this.header === 3) && b === 0x0a) {
this.header++;
} else {
this.header = 0;
}
if (this.header === 4) {
this.connected = true;
this.onConnect();
this.header = 5;
}
} else {
if (this.dataState === 0) {
this.dataState = b & 0x80 === 0x80 ? 2 : 1;
// Low bit frame
} else if (this.dataState === 1) {
if (b === 0xff) {
var buffer = new Buffer(this.dataFrames);
this.dataFrames = [];
this.dataState = 0;
if (!this.message(buffer.toString('utf8', 0, buffer.length))) {
this.send({error: 'Invalid Message.'});
this.close();
return;
}
} else {
this.dataFrames.push(b);
}
// Unused high bit frames
} else if (this.dataState === 2) {
if (b === 0x00) {
this.close();
}
}
}
}
},
write: function(data) {
var bytes = 0;
if (!this.socket.writable) {
return bytes;
}
try {
this.socket.write('\x00', 'binary');
if (typeof data === 'string') {
this.socket.write(data, 'utf8');
bytes += Buffer.byteLength(data);
}
this.socket.write('\xff', 'binary');
this.socket.flush();
bytes += 2;
} catch(e) {}
this.bytesSend += bytes;
return bytes;
},
message: function(msg) {
if (this.decoder) {
try {
msg = this.decoder(msg);
} catch(e) {
this.close();
return false;
}
}
this.onData(msg);
return true;
}
};
在這里我們設置它:
var bison = require('bison');
// automatically do some encoding/decoding magic
var test = new WebSocket('localhost', 28785, bison.encode, bison.decode);
test.onConnect = function() {
};
test.onData = function(data) {
};
隨意在評論中提問。
PS:有關其工作原理的信息,請閱讀規范:P
看看wiki上的模塊頁面 ,它有一些用於在npm中可用的websocket客戶端的模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.