S
S
Space2014-03-11 15:01:06
JavaScript
Space, 2014-03-11 15:01:06

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) {
      }
    });
   }
   });


And the server code:
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!');
    }
  });
});


I don’t need solutions in my code, I just don’t understand how to pass a variable in the client 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

2 answer(s)
S
Space, 2014-03-12
@ruslite

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 );

Q
q1t, 2014-03-11
@q1t

Date.now() returns anything?
and so will work (as on the client)?

var dateObj = new Date();
var aqwsde = dateObj.getTime() - socketEmitLastCallTime;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question