var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
console.log( "New client !" );
var timing={};
client.on( 'message', function( data ) {
var time=0;
if (!!timing[data.name])
time=timing[data.name]-Date.now()+2000; //Задержка 2 сек.
setTimeout(function() {
timing[data.name]=Date.now();
console.log( 'Message received ' + data.name + "1" + data.message + data.canal );
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.sockets.emit( 'message', { name: data.name, message: data.message, canal: data.canal } );
},time);
});
});
server.listen( 8080 );
Получилось так: отправил сообщение. Потом отправляю еще одно, и если не прошло 2сек, то сервер ждет пока они пройдут, а потом отправляет. Что не так? var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
console.log( "New client !" );
client.on( 'message', function( data ) {
console.log( 'Message received ' + data.name + "1" + data.message + data.canal );
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.sockets.emit( 'message', { name: data.name, message: data.message, canal: data.canal } );
});
});
server.listen( 8080 );
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
io.sockets.on( 'connection', function( client ) {
console.log( "New client !" );
client.on( 'message', function( data ) {
console.log( 'Message received ' + data.name + ":" + data.message + data.canal );
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.sockets.emit( 'message', { name: data.name, message: data.message, canal: data.canal } );
});
});
server.listen( 8080 );