在连接build立之前,socket.io websocket被closures

当我连接服务器时,浏览器中的控制台打印出这个信息。 我仍然能够从服务器端发出事件,并将其caching在客户端,但我无法从客户端发出它们。

服务器端:

var express = require('express'); var http = require('http'); var app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); server.listen(8080); app.get("/", function(req, res) { res.sendFile(__dirname + '/index.html'); }); io.on('connection', function(socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); 

客户端:

 <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Untitled Document</title> </head> <body> <h1 id ="id">TUTAJ JESTEM</h1> <script src="https://cdn.socket.io/socket.io-1.3.7.js"></script> <script> var socket = io.connect('http://localhost:8080'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); window.onload=socket; </script> </body> </html> 

       

网上收集的解决方案 "在连接build立之前,socket.io websocket被closures"