在Socket.io中与HTTP通信TCP获取TypeError:无法读取未定义的属性“emit”

尝试与HTTP服务器通信TCP服务器

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

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

在TCP端口上收到的数据显示在控制台窗口上,我试图通过将数据存储在全局variables中将此数据传递给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> 

问题是你试图在net.Socket中使用socket.io 广播 ,这当然没有这个属性。

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

当一个新的TCPstream被build立时。 socket是net.Sockettypes的对象。 通常用户不会想要访问这个事件。 特别是,由于协议parsing器如何连接到套接字,套接字不会发出“可读”的事件。 套接字也可以通过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. } 
 function broadcast(message, sender) { MYVAR2 = message; console.log(MYVAR2); sender.broadcast.emit('updateHeader',MYVAR2); //Replace socket by sender here }