socket.io不能在客户端工作(传输错误)

我试图按照教程https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel- 51 。 我得到了node.js运行,reddis工作和端口3000正在监听。 我可以在命令行上获取控制台消息。 当我尝试向客户端发送任何内容时会发生问题。 切断方:

var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var Redis = require('ioredis'); var redis = new Redis(); redis.subscribe('test-channel', function(err, count) { }); redis.on('message', function(channel, message) { console.log('Message Recieved: ' + message); message = JSON.parse(message); io.emit(channel + ':' + message.event, message.data); }); http.listen(3000, function(){ console.log('Listening on Port 3000'); }); 

客户端:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"></script> <script> var socket = io('http://localhost:3000'); //var socket = io('http://192.168.10.10:3000'); socket.on("test-channel:App\\Events\\EventName", function(message){ console.log("Working"); // increase the power everytime we load test route $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power)); }); 

我试图通过浏览器控制台使用“localStorage.debug ='*';” 并收到以下消息:

 engine.io-client:socket socket error {"type":"TransportError","description":{}} +4ms socket.io-client:manager connect_error +4ms socket.io-client:manager reconnect attempt error +1ms socket.io-client:manager will wait 5000ms before reconnect attempt +0ms engine.io-client:socket socket close with reason: "transport error" +1ms engine.io-client:polling transport not open - deferring close +1ms socket.io-client:manager attempting reconnect +5s socket.io-client:manager readyState closed +1ms 

我尝试了所有可用的示例,我也尝试了来自socket.io网站的基本示例,没有任何工作。 任何帮助表示赞赏。 谢谢

经过几天的挖掘,我发现socket.io在本地主机上工作得非常好,但是从extrernal域不起作用。 我也试图改变

 var socket = io('http://localhost:3000'); 

 var socket = io('http://serverip:3000'); 

但它没有帮助。 Chrome浏览器给我错误:

 GET http://serverip:3000/socket.io/?EIO=3&transport=polling&t=LDrVJCA Request.create @ socket.io.min.js:1Request @ socket.io.min.js:1XHR.request @ socket.io.min.js:1XHR.doPoll @ socket.io.min.js:1Polling.poll @ socket.io.min.js:1Polling.doOpen @ socket.io.min.js:1Transport.open @ socket.io.min.js:1Socket.open @ socket.io.min.js:1Socket @ socket.io.min.js:1Socket @ socket.io.min.js:1Manager.open.Manager.connect @ socket.io.min.js:2(anonymous function) @ socket.io.min.js:3 feedTest:1 XMLHttpRequest cannot load http://serverip:3000/socket.io/?EIO=3&transport=polling&t=LDrVJCA. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access. The response had HTTP status code 502. 

我真的很感激任何帮助。 谢谢

       

网上收集的解决方案 "socket.io不能在客户端工作(传输错误)"

这是因为CORS

你可以使用jsonp来避免它:

  $.ajax({ type: "POST", dataType: 'jsonp', ...... etc .... }) 

编辑

对于套接字io做这个:

 var socket = io('http://localhost:3000'); socket.set("origins","*"); 

编辑

 var socket = io('http://localhost:3000',{origins:'localhost:* http://localhost:* http://www.localhost:*'}); 

要么

 var socket = io('http://localhost:3000',{origins:"*"}); 

在laracasts.com的帮助下,我设法解决了这个问题。 港口3000被closures的宣传,这就是为什么它只在本地主机上工作。 感谢alexey试图帮助。