我是Node JS的新手。我写了NodeJs代码它运行正常。但是当从客户端发送消息时发送,但我想重复发送消息。
客户端发送任何消息时发送响应:
var webSocketServer = require('ws').Server;
var http = require('http');
var fs = require('fs');
var webSocketServerObject = new webSocketServer({ port: 9060 });
webSocketServerObject.on('connection', function (socketObject) {
socketObject.on('message', function (message) {
console.log('The' + message + 'Message Received from \n from IP ' + socketObject.upgradeReq.connection.remoteAddress);
socketObject.send("Received " + message);
});
socketObject.on('close', function (c, d) {
console.log('Disconnect ' + c + ' -- ' + d);
});
});
上面的代码工作正常(reference)我根据间隔修改了相同的代码,但它不起作用。 以下修改:
webSocketServerObject.on('connection', function (socketObject) {
setInterval(function(){
socketObject.send("Hello...");
}, 10000);
socketObject.on('close', function (c, d) {
console.log('Disconnect ' + c + ' -- ' + d);
});
});
我如何在服务器中继续发送消息。