2012-04-05 434 views
23

我想要開始使用node.js和socket.io。
我有一個非常(非常)簡單的客戶端服務器測試應用程序,我不能讓它工作。
該系統設置在帶有apache服務器的Windows機器上。
的Apache是​​在port 81運行,並且在socket.io設置聽port 8001socket.io - 'connect'事件不會在客戶端觸發

服務器 - server.js

var io = require('socket.io').listen(8001); 

io.sockets.on('connection', function (socket) { 
    console.log('\ngot a new connection from: ' + socket.id + '\n'); 
}); 

客戶 - index.html的

<!DOCTYPE html> 
<html> 
<head> 
    <title>node-tests</title> 
    <script type="text/javascript" src="http://localhost:8001/socket.io/socket.io.js"> </script> 
    <script type="text/javascript"> 
     var socket = io.connect('http://localhost', { port: 8001 }); 

     socket.on('connect', function() { 
      alert('connect'); 
     }); 

     socket.on('error', function (data) { 
      console.log(data || 'error'); 
     }); 

     socket.on('connect_failed', function (data) { 
      console.log(data || 'connect_failed'); 
     }); 
    </script> 
</head> 
<body> 

</body> 
</html> 

使用node server.js啓動服務器後,標準(預期)輸出爲wr伊頓服務器控制檯:

info: socket.io started 

當我在瀏覽器中打開index.html(鉻在我的情況 - 在其他瀏覽器沒有測試)以下日誌寫入服務器控制檯:

info: socket.io started 
debug: served static content /socket.io.js 
debug: client authorized 
info: handshake authorized 9952353481638352052 
debug: setting request GET /socket.io/1/websocket/9952353481638352052 
debug: set heartbeat interval for client 9952353481638352052 
debug: client authorized for 

got a new connection from: 9952353481638352052 

debug: setting request GET /socket.io/1/xhr-polling/9952353481638352052?t=1333635235315 
debug: setting poll timeout 
debug: discarding transport 
debug: cleared heartbeat interval for client 9952353481638352052 
debug: setting request GET /socket.io/1/jsonp-polling/9952353481638352052?t=1333635238316&i=0 
debug: setting poll timeout 
debug: discarding transport 
debug: clearing poll timeout 
debug: clearing poll timeout 
debug: jsonppolling writing io.j[0]("8::"); 
debug: set close timeout for client 9952353481638352052 
debug: jsonppolling closed due to exceeded duration 
debug: setting request GET /socket.io/1/jsonp-polling/9952353481638352052?t=1333635258339&i=0 
... 
... 
... 

在瀏覽器控制檯,我得到:

connect_failed 

所以它看起來像客戶端到達服務器,並嘗試連接,服務器被授權握手,但connect事件永遠不會在客戶端觸發,而是連接方法會達到其connect timeoutmax reconnection attempts,並放棄返回connect_failed

任何幫助\對此的見解將有所幫助。

謝謝

+7

只用io.connect(),它可以推斷嘗試來自您所在網址的網址和端口。我遇到過問題,io.connect中的事情並不像我期望的那樣工作。 – 2012-04-05 18:45:26

+1

@kfiroo你有沒有得到任何解決方案? – Hackableweb 2013-07-19 01:11:07

+0

@TimothyStrimple謝謝蒂姆! **再次在heroku(Cedar)和localhost **上工作! – AmpT 2014-04-10 23:34:23

回答

0

嘗試以下操作:

變化

var socket = io.connect('http://localhost', { port: 8001 }); 

var socket = io.connect('http://localhost:8001'); 
2

變化在前端(客戶端):

var socket = io.connect(http://myapp.herokuapp.com);

var socket = io.connect();

很多時間處理那些沒有正確連接註冊到服務器的各種客戶端(瀏覽器,火狐瀏覽器和應用程序的PhoneGap)後(即服務器在其日誌中成功註冊了websocket連接,但前端沒有註冊連接事件),我試着從評論中提取@ Timothy的解決方案,現在所有的都再次在heroku和localhost上運行。

4
//Client side 
<script src="http://yourdomain.com:8001/socket.io/socket.io.js"></script> 
var socket = io.connect('http://yourdomain.com:8001'); 

//Server side 
var io = require('socket.io').listen(8001); 
io.sockets.on('connection',function(socket) { 
`console.log('a user connected');` 
}); 

客戶端: 客戶端代碼請求從,讓你的服務器端socketIO相同的文件插座IO的客戶端版本。 Var套接字現在具有socketIO提供的所有方法,例如socket.emit或socket.on

服務器端: 與客戶端相同,使得socketIO方法可在var io下使用。

+0

歡迎來到StackOverflow。你能解釋一下這個代碼嗎?它可以幫助未來的遊客。 – 2014-09-15 17:33:44

+0

這是使用node.js連接最簡單的方法。 – 2015-07-21 13:09:13

0
// server 
var server = http.createServer(app).listen(8001); 

// client 
var socket = io.connect(); 
0

嘗試使用這樣的:

Server.js

var http = require("http").createServer(), io = require("socket.io").listen(http); 

http.listen(8001); 

io.sockets.on("connection", function(socket){ 
    console.log("Connected!"); 
    socket.emit("foo", "Now, we are connected"); 
}); 

的index.html

<script src="http://localhost:8001/socket.io/socket.io.js"></script> 
<script> 
    var i = io.connect("http://localhost:5001"); 
    i.on("foo", function(bar){ 
     console.log(bar); 
    }) 
</script>