1

Trying to communicate TCP server with HTTP server

My TCP port is 4040 and HTTP port is 3000

I am working on passing data received on TCP server to HTTP server

Data received on TCP port is showing on console window and I am trying to pass this data to HTTP by storing data in global var so that I can display it on the webpage.

Thanks :)

server code:

enter code here    var http = require('http').createServer(httpHandler);
var net = require('net');
var app = require('express')();         <!-- These are mandatory variables -->
var http = require('http').Server(app);
var io = require('socket.io')(http);
var sockets = [];
var HOST = 'localhost';
var PORT = 4040;
global.MYVAR = "Hello world";
global.MYVAR2 = "Hello world";
var server = net.createServer();
server.listen(PORT, HOST);
// Keep track of the chat clients
var clients = [];
/**
* http server
*/
function httpHandler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
    if (err) {
    res.writeHead(500);
    return res.end('Error loading index.html');
    }

res.writeHead(200);
res.end(data);
});
}

app.get('/', function(req, res){           <!-- This sends the html file -->
//send the index.html file for all requests
  res.sendFile(__dirname + '/index.html');

});

http.listen(3000, function(){    <!-- Tells the HTTP server which port to use -->

  console.log('listening for HTTP on *:3000');   <!-- Outputs text to the console -->
  console.log('listening for TCP on port ' + PORT);
});

<!-- everything below this line is actual commands for the actual app -->

io.on('connection', function(socket) // Opens the socket

{
  socket.on('checkbox1', function(msg){  // Creates an event
     console.log(msg); // displays the message in the console
     MYVAR = msg; // Sets the global variable to be the contents of the message recieved
     for (var i = 0; i < sockets.length; i++) {
      if(sockets[i]) {
        sockets[i].write(MYVAR, 'utf-8');
      }
    }
  });

});


server.on('connection', function(socket){ // Opens the socket for the TCP connection
    sockets.push(socket);
    socket.write(MYVAR, 'utf-8');


    // Handle incoming messages from clients.
    socket.on('data', function (data) {

         broadcast(socket.name + "> " + data, socket);

    });
    // Send a message to all clients
function broadcast(message, sender) {

    MYVAR2 = message;
     console.log(MYVAR2);
     socket.broadcast.emit('updateHeader',MYVAR2); // GETTING ERROR HERE

}
  }).listen(PORT, HOST);

index.html code:

<!doctype html>
<html>
<head>
<title>Socket IO Test</title>
</head>
<body>
<h1 id="h1">Hello World</h1>
<form action="">
<input type='checkbox' onclick='checkbox1(this);'>Checkbox1</label>
</form>

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
 var socket = io();
 var number = 0;

 $(document).ready(function(){
 socket.on('updateHeader',function(data){
 console.log('updateHeader called');
 document.getElementById('h1').innerHTML = data;
 });
 });

 function checkbox1(cb) {
 socket.emit('checkbox1', 'checkbox 1 = ' + cb.checked);
 return false;
 }
 </script>

2 Answers 2

1

The problem is you're trying to use socket.io broadcast in a net.Socket which of course doesn't have that property.

server.on('connection', function(socket){ /* ... */ }

When a new TCP stream is established. socket is an object of type net.Socket. Usually users will not want to access this event. In particular, the socket will not emit 'readable' events because of how the protocol parser attaches to the socket. The socket can also be accessed at request.connection.

I don't know exactly what you're trying to achieve, but you can use io.emit if you want to send message to all clients.

function broadcast(message, sender) {
    MYVAR2 = message;

    //This will emit 'updateHeader' to all socket.io connected sockets
    io.emit('updateHeader', MYVAR2); 

    //The 'socket' you were using here was a net.Socket not a socket.io one.
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow man!God bless you! don't know how to thank you! I was trying this for like two weeks.I am new to node.js.Can you suggest me any good book or site where I can learn socket.io
You're welcome! I learned from many different sites, so google and nodejs docs, nodejs.org/dist/latest-v7.x/docs/api
0
function broadcast(message, sender) {
    MYVAR2 = message;
     console.log(MYVAR2);
     sender.broadcast.emit('updateHeader',MYVAR2); //Replace socket by sender here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.