Событие закрытия Websocket не запускается на сервере - node.js - PullRequest
0 голосов
/ 17 января 2019

Я использую плагин ws в приложении nodejs для реализации веб-сокета.

Когда я закрываю соединение ws с клиента, событие ws close должно вызывать. Он работает на локальном сервере, но не работает на сервере.

Вот пример кода:

exports.Initialize = function (server, app) {
	wss = new WebSocket.Server({
		server
	});
	app.on('upgrade', wss.handleUpgrade);
	setWsHeartbeat(wss, (ws, data, binary) => {
		if (data === '{"kind":"ping"}') { // send pong if recieved a ping.
			ws.send('{"kind":"pong"}');
		}
	});
	wss.on('connection', function connection(ws, req) {

		clients.push({
			ws,
			type: location.query.type
		});

		ws.on('message', function (response) {

		});

		ws.on('close', function (e, c, b, x) {
			let index = clients.findIndex(x => x.ws === ws);
			if (index > -1) {
				clients.splice(index, 1);
			}
		});

		ws.on('error', function (e) {
			let index = clients.findIndex(x => x.ws === ws);
			if (index > -1) {
				clients.splice(index, 1);
			}
		});
	});
};
...