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

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

正在尝试将 TCP 服务器与 HTTP 服务器通信

我的TCP端口是4040,HTTP端口是3000

我正在努力将 TCP 服务器上接收到的数据传递到 HTTP 服务器

在 TCP 端口上接收到的数据显示在控制台上 window,我试图通过将数据存储在全局变量中来将此数据传递给 HTTP,以便我可以在网页上显示它。

谢谢:)

服务器代码:

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代码:

<!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>

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

问题是您正在尝试使用 socket.io broadcast in a net.Socket 当然没有 属性.

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.

我不知道你到底想达到什么目的,但如果你想向所有客户端发送消息,你可以使用 io.emit

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