我正在尝试按照教程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));
});
我尝试使用&#34; localStorage.debug =&#39; *&#39;;&#34;通过浏览器控制台进行调试。我收到以下消息:
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在localhost上运行得很好,但是从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.
我真的很感激任何帮助。感谢
答案 0 :(得分:1)
这是因为CORS
您可以使用 jsonp 来避免它:
$.ajax({
type: "POST",
dataType: 'jsonp',
...... etc ....
})
修改强>
对于socket 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:"*"});
答案 1 :(得分:0)
在laracasts.com的帮助下,我设法解决了这个问题。 3000港口因宣传而关闭,这就是为什么它只适用于localhost。感谢alexey试图提供帮助。