【发布时间】:2015-01-25 20:34:42
【问题描述】:
我正在使用使用 socket.io 与 iOS 客户端通信的 nodejs express 服务器,并且在尝试测试有多少客户端可以随时连接和交换数据时遇到了一些麻烦。
我的目标是能够运行一个脚本,通过数千个不同的会话连接到 socket.io,以及发送和接收数据以了解我们系统的规模。目前我们在 Heroku 上使用单个 dyno,但可能很快会考虑在 AWS 上使用其他选项。
我找到了应该为早期版本的 socket.io 做的代码,例如this,但由于 v1.x 似乎有一个非常不同的握手协议,所以出现了问题。我尝试使用socket.io-client包,但尝试多次连接只模拟使用一个会话,我需要模拟多个独立用户。
我一直在分解 socket.io-client 代码,但只到创建连接为止 - 我被困在发送数据部分。如果有人有任何知识或可以指出一些关于如何在客户端和 socket.io 服务器之间发送数据的书面资源,那将对我有很大帮助。
这是我目前所拥有的:
var needle = require('needle'),
WebSocket = require('ws'),
BASE_URL = 'url-to-socket-host:5002';
var connectionNo = 0;
needle.get('http://' + BASE_URL + '/socket.io/?EIO=3&transport=polling&t=1416506501335-0', function (err, resp) {
// parse the sid
var resp = JSON.parse(resp.body.toString().substring(5, resp.body.toString().length));
// use the sid to connect using websockets
var url = 'ws://' + BASE_URL + '/socket.io/?EIO=3&transport=websocket&sid=' + resp.sid;
console.log(connectionNo + ' with sid: ' + resp.sid);
var socket = new WebSocket(url, void(0), {
agent: false
});
socket.on('open', function () {
console.log('Websocket connected: ' + connectionNo);
// I don't understand how to send data to the server here,
// from looking at the source code it should use some kind
// of binary encoding, any ideas?
socket.on('message', function (msg) {
console.log(msg);
});
});
});
我将继续解构 socket.io-client 代码,但如果有人有任何可能有帮助的线索或资源,请告诉我。谢谢。
【问题讨论】:
-
太好了,谢谢 - 我一定错过了。
标签: node.js sockets heroku socket.io