Io和Angular。刷新页面时,不会触发socket.io中的disconnect事件。我在网上完成了一些研究,这使我实现了以下
io.set('transports', ['xhr-polling']);
但是,当设置此项时,我会收到以下错误;
获取http://app.local:3000/socket.io/?EIO=3&transport=polling&t=1448414100223-67 400(错误请求)
我有以下代码;
server.js
var express = require('express'),
http = require('http'),
server = http.createServer(app),
path = require('path'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser');
var app = express();
const redis = require('redis');
const io = require('socket.io').listen(server);
const client = redis.createClient();
server.listen(3000, '0.0.0.0');
io.listen(server).on('connection', function(client) {
const redisClient = redis.createClient();
redisClient.subscribe('user.joined');
redisClient.on("message", function(channel, message) {
client.emit(channel, message);
});
client.on('disconnect', function() {
redisClient.quit();
});
});
io.set('transports', ['xhr-polling']);
角度代码;
var serverBaseUrl = 'http://app.local:3000';
myApp.factory('socket', function (socketFactory) {
var myIoSocket = io.connect(serverBaseUrl, {'sync disconnect on unload' : true});
var socket = socketFactory({
ioSocket: myIoSocket
});
return socket;
});
任何想法我做错了什么?
答案 0 :(得分:3)
嗯,是的,这可能会发生,不幸的是,polling
宁愿...... 故障安全传输,如果其他人失败,应该使用它。
首先,我建议为套接字定义几个传输来使用。
// server
io.set('transports', [ 'websocket', 'flashsocket', 'polling' ] );
// client
io.connect( "/", { secure: true, transports: [ "flashsocket","polling","websocket" ] } );
您定义它们的顺序很重要 - socket.io将尝试按照您定义的相应顺序使用它们。顺便说一下,由于socket.io
1.0 polling
xhr-polling
而非disconnect
。
为了尽量减少未收到onbeforeunload
活动的情况,我可以推荐使用浏览器窗口window.onbeforeunload = function() {
console.log( "unloading resources" );
$scope.socket.disconnect();
$scope.socket.close();
};
事件,并添加类似的内容
connect
您可以在客户端收到 datatype: "json",
contentType: 'application/json',
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
serializeGridData: function (postData) { return JSON.stringify(postData); },
width: gwdth - 30,
height: 580,
colNames: ['ID', 'Icon', 'Path', 'Source', 'Name', 'Severity', 'Resolution State', 'Age', 'Created Date', 'Repeat Count'],
colModel: [
{ name: 'id', hidden: true, width: 1, key: true },
{ name: 'severity', width: 20, edittype: 'image', formatter: imageIcon, align: "center" },
{ name: 'path', width: 30 },
{ name: 'source', width: 30 },
{ name: 'name', width: 30 },
{ name: 'severity', width: 30, hidden: true },
{ name: 'resolutionState', width: 30 },
{ name: 'age', width: 30 },
{ name: 'createdDate', width: 30, formatter: 'date', formatoptions: { srcformat: 'y-m-d', newformat: 'l, F d, Y' } },
{ name: 'occuranceCount', width: 30, align: "center" }
],
loadonce: true,
rowNum: 25,
rowList: [20, 30, 50],
gridview: true,
mtype: 'GET',
sortname: 'ID',
sortorder: 'desc',
viewrecords: true,
sortable: true,
pager: "#jqGridPagerA2log",
grouping: true,
groupingView: {
groupField: ['severity'],
groupColumnShow: [true],
groupText: ['<b>{0}({1})</b>'],
groupOrder: ["asc"],
groupSummary: false,
groupCollapse: true
}
});
事件后立即添加此事件处理程序。
希望这有帮助。