Answer the question
In order to leave comments, you need to log in
How to display the time on the server?
Hello. There is a chat on sockets and node.js. I made the time of the message to be displayed, but this is on the client. How to do it on the server?
Here is the client code:
var socket = io.connect( 'http://localhost:8080' );
$( "#messageForm" ).submit( function(e) { e.preventDefault();
var nameVal = $( "#nameInput" ).val();
var msg = $( "#messageInput" ).val();
var canalVal = $( "#canalInput" ).val();
var t=new Date();
var h=t.getHours();
var m=t.getMinutes();
var s=t.getSeconds();
if(h < 10){
h = '0' + t.getHours();
}
if(m < 10){
m = '0' + t.getMinutes();
}
if(s < 10){
s = '0' + t.getSeconds();
}
var result=h+":"+m+":"+s;
if(msg) {
socket.emit( 'message', { name: nameVal, message: msg, canal: canalVal, timing: result } )
$( "#messageInput" ).val('');
// Ajax call for saving datas
$.ajax({
url: "./ajax/insertNewMessage.php",
type: "POST",
data: { name: nameVal, message: msg, canal: canalVal, timing: result },
success: function(data) {
}
});
}
});
var socket = require( 'socket.io' ),
express = require( 'express' ),
http = require( 'http' ),
app = express(),
server = http.createServer( app ),
io = socket.listen( server ),
socketEmitLastCallTime = 0
io.sockets.on( 'connection', function( client ) {
client.broadcast.emit('clientNew', {clientNew : 'Пришел новый Вася!'});
client.on( 'message', function( data ) {
var aqwsde = Date.now() - socketEmitLastCallTime;
if (aqwsde > 1100){
socketEmitLastCallTime = Date.now(); //опять эта переменная не записывается, что не так?
console.log( 'Message received ' + data.timing + data.name + "1" + data.message + data.canal );
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.sockets.emit( 'message', { timing: data.timing, name: data.name, message: data.message
.replace(/&/g,"&")
.replace(/</g,"<")
.replace(/>/g,">")
.replace(/"/g,'"')
.replace(/'/g,"'")
.replace(/\//g,"⁄")
, canal: data.canal } );
}else{
console.log('TIMEEEEE!');
}
});
});
timing: result
, which, according to the idea, should be on the server. How to do?
Answer the question
In order to leave comments, you need to log in
It was a dumb question. This is what it means to sit and do at night.
Who cares:
var socket = require( 'socket.io' ),
express = require( 'express' ),
http = require( 'http' ),
app = express(),
server = http.createServer( app ),
io = socket.listen( server ),
socketEmitLastCallTime = 0
io.sockets.on( 'connection', function( client ) {
client.broadcast.emit('clientNew', {clientNew : 'Пришел новый Вася!'});
client.on( 'message', function( data ) {
var t=new Date(),
h=t.getHours(),
m=t.getMinutes(),
s=t.getSeconds(),
result=h+":"+m+":"+s,
aqwsde = Date.now() - socketEmitLastCallTime
if (aqwsde > 1100){
socketEmitLastCallTime = Date.now(); //опять эта переменная не записывается, что не так?
console.log( 'Message received ' + result + data.name + "1" + data.message + data.canal );
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.sockets.emit( 'message', { result : result, name: data.name, message: data.message
.replace(/&/g,"&")
.replace(/</g,"<")
.replace(/>/g,">")
.replace(/"/g,'"')
.replace(/'/g,"'")
.replace(/\//g,"⁄")
, canal: data.canal } );
}else{
console.log('TIMEEEEE!');
}
});
});
server.listen( 8080 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question