lupa
lupa

Reputation: 11

How to pass parameters in socket

I need to pass parameters in socket and I use the method emit.
Server Side:

var http=require('http');

var numero_giocatori=0;

var express = require('express');
var app = express();

var net = require('net');

var client = new net.Socket();

var vettore_clienti=[];
var chatServer = net.createServer();


chatServer.on('connection', function(client) {
    numero_giocatori=numero_giocatori+1;
    vettore_clienti.push(client);
    if(numero_giocatori===2){
        console.log("IScritto "+numero_giocatori);
        for(var i=0;i<vettore_clienti.length;i++){
            vettore_clienti[i].emit('vettore_clienti',vettore_clienti);
        }
    }
});
chatServer.listen(8000,'127.0.0.1');

But I don't know how read the value that I passed first with emit method.
Client Side:

var net = require('net');

var client = new net.Socket();

client.connect(8000, '127.0.0.1', function() {

});

client.on('data', function(data) {
console.log(data);
});

Upvotes: 1

Views: 4699

Answers (2)

Raphael Schweikert
Raphael Schweikert

Reputation: 18556

Node’s TCP sockets are meant to be universal (the other end does not have to be a Node program) and do not prescribe any standards for how your messages are formatted. In other words: you’re free to create your own encoding for messages.

Or you can simply use JSON (if you don’t care about the overhead this creates when compared to an encoding that is better adapted to your specific use case):

Server

var http=require('http');

var numero_giocatori=0;

var express = require('express');
var app = express();

var net = require('net');

var client = new net.Socket();

var vettore_clienti=[];
var chatServer = net.createServer();

function sendMessage(client, messageType) {
    var message = {type: messageType, data: [].slice.call(arguments, 2)};
    client.write(JSON.stringify(message)); // Sending a string implicitly converts it into a buffer using utf-8
}


chatServer.on('connection', function(client) {
    console.log('Got connection', client);
    numero_giocatori=numero_giocatori+1;
    vettore_clienti.push(client);
    if(numero_giocatori===2){
        console.log("IScritto "+numero_giocatori);
        for(var i=0;i<vettore_clienti.length;i++){
            sendMessage(vettore_clienti[i], 'welcome', "You are client number", i+1, 'of', vettore_clienti.length);
        }
    }
});
chatServer.listen(8000,'127.0.0.1');

Client

var net = require('net');

var client = new net.Socket();

client.connect(8000, '127.0.0.1', function() {
    console.log('Connected');
});

client.on('data', function(data) {
    var message = JSON.parse(data.toString()); // calling toString() with no arguments assumes the buffer is in utf-8
    var messageType = message.type;
    var data = message.data;
    console.log.apply(console, data);
});

Note that this way you are restricted to sending stuff that can actually be represented in JSON. This excludes functions, and values like NaN or Infinity. Sending an array of clients complete with functions and streams like you tried in your sample code thus won’t work.

Upvotes: 0

dm03514
dm03514

Reputation: 55962

I'm not sure if node.js net socket library has an emit method.

The docs suggest that sending data over the socket is done through the sockets write method.


socket.io would allow you to create websocket connections, and to emit and listen for arbitrary events:

http://socket.io/docs/server-api/#server#emit

Upvotes: 1

Related Questions