我試圖按照教程https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel-51來實現套接字。 我得到了node.js運行,reddis工作和端口3000正在監聽。我可以在命令行上獲得控制檯消息。當我嘗試向客戶端發送任何東西時會發生問題。 Sever的側:socket.io不能在客戶端工作(傳輸錯誤)
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.
我真的很感激任何幫助。由於
您能否提供完整的示例,因爲我不知道在哪裏插入建議的代碼。謝謝 – user3549162
已更新的答案,請參閱 – alexey
socket.set不是在插座1.x版本中刪除的功能。你有什麼建議嗎?先謝謝你。 – user3549162