尝试使用 socketcluster 在浏览器窗口之间交换事件。
在发件人方面,我有:
var options = {
hostname: "myserver.com",
secure: true,
port: 443,
connectTimeout: 86400000,
autoReconnectOptions: {
initialDelay: 100, //milliseconds
randomness: 10, //milliseconds
multiplier: 1.5, //decimal
maxDelay: 60000 //milliseconds
}
};
// Initiate the connection to the server
var socket = socketCluster.connect(options);
socket.on('connect', function () {
console.log('CONNECTED');
});
function sendTime() {
var currentDate = new Date();
var theId = document.getElementById("object_id").value;
count++;
console.log("id "+theId);
socket.emit('identity1', { timestamp: currentDate, id: theId, counter:count});
}
然后在服务器上,我让工作人员发布一个新事件:
socket.on('identity1', function (data) {
count++;
console.log('Handled identity1', data);
scServer.exchange.publish('identity-' + data.id, data);
});
在接收方我有:
// Initiate the connection to the server
var socket = socketCluster.connect(options);
socket.on('connect', function () {
console.log('CONNECTED');
identityChannel = socket.subscribe('identity-' + document.getElementById("object_id").value);
identityChannel.watch(function (data) {
var theTime=data.timestamp;
console.log('ID:' + data.id + ' TIME: ' + theTime);
document.getElementById("data2").innerHTML = 'TIME: ' + theTime + 'COUNTER : ' + data.counter ;
});
});
在 Chrome 的 js 控制台中,我看到双方 10 秒后,客户端连接被拒绝:
socketcluster.js:678 Uncaught SocketProtocolError {name: "SocketProtocolError", message: "Socket hung up", code: 1006, stack: "SocketProtocolError: Socket hung up↵ at SCSocke…myserver.com/socketcluster.js:1392:10)"}
(anonymous) @ socketcluster.js:678
setTimeout (async)
SCSocket._onSCError @ socketcluster.js:676
SCSocket._onSCClose @ socketcluster.js:781
(anonymous) @ socketcluster.js:426
Emitter.emit @ socketcluster.js:4152
SCTransport._onClose @ socketcluster.js:1494
wsSocket.onclose @ socketcluster.js:1392
sender.html:26 CONNECTED
我看到重新连接时某些事件丢失了。
问:这正常吗?
问:10s的限制可以调吗?