R.zeiwald
R.zeiwald

Reputation: 236

Communicating TCP with HTTP in Socket.io getting TypeError: Cannot read property 'emit' of undefined

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>

Upvotes: 1

Views: 815

Answers (2)

Marcos Casagrande
Marcos Casagrande

Reputation: 40444

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.
}

Upvotes: 1

Julien Klepatch
Julien Klepatch

Reputation: 348

function broadcast(message, sender) {
    MYVAR2 = message;
     console.log(MYVAR2);
     sender.broadcast.emit('updateHeader',MYVAR2); //Replace socket by sender here
}

Upvotes: 0

Related Questions