2011-03-30 72 views
2

大家好我有問題....插座與節點JS例如

客戶端代碼

<html> 
    <body onload="fun()"> 
    <script src="C:\cygwin\usr\local\lib\node\.npm\socket.io\0.6.16\package\support\socket.io-client\socket.io.js"></script> 
    <script> 

    function fun() 
    { 
    alert("hello") 
    var socket = new io.Socket('localhost',{'port':8090}); 

    socket.on('connect', function(){ 
     socket.send('hi!'); 
    }) 

    socket.on('connect', function(){ console.log('connected to server');socket.send('hi there, this is a test message'); }) 


    socket.on('message', function(data){ 
     alert(data); 
    }) 
    socket.on('disconnect', function(){}) 
    } 
    </script> 
    </body> 
    </html> 

服務器端代碼:

var http = require('http'), 
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') 

server = http.createServer(function(req, res){ 
// your normal server code 
res.writeHead(200, {'Content-Type': 'text/html'}); 
res.end('<h1>Hello world</h1>'); 
}); 
server.listen(8090); 

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
    // new client is here! 
    client.on('message', function(){ console.log('message arrive'); }) 

    client.on('disconnect', function(){ console.log('connection closed');}) 

}); 

發現從socket.io這個例子。 .. 當我運行服務器,它給我的套接字io準備好了...接受連接 當我運行瀏覽器時,它不顯示anthing和firfox螢火蟲控制檯plz幫助我解決這個問題.....

回答

4

你永遠不會在客戶端調用socket.connect(),所以套接字永遠不會嘗試連接到服務器。請檢查下面的代碼:

客戶端 - >

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 
<script src="socket.io.js"></script> 
<script> 

function fun() 
{ 
    var socket = new io.Socket('localhost',{'port':8090}); 

    socket.connect(); 

    socket.on('connect', function(){ 
     console.log('connected'); 
     socket.send('hi!'); 
    }); 


    socket.on('message', function(data){ 
     console.log('message recived: ' + data); 
    }); 

    socket.on('disconnect', function(){ 
     console.log('disconected'); 
    }); 
} 

$(document).ready(function() { 
    fun(); 
}); 
</script> 
</head> 
<body> 
</body> 
</html> 

服務器端 - >

var http = require('http'), 
    io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io') 

server = http.createServer(function(req, res){ 
    // your normal server code 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('<h1>Hello world</h1>'); 
}); 
server.listen(8090); 

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
    // new client is here! 
    client.on('message', function(){ 
     console.log('message arrive'); 
     client.send('some message'); 
    }); 

    client.on('disconnect', function(){ 
     console.log('connection closed'); 
    }); 

}); 
+0

感謝答覆,但我面臨同樣的問題......這些代碼是爲你工作? ??我運行這樣的代碼:.....在cygwin runserver節點server.js上,然後運行html文件,這是運行的正確方式 – 2011-03-30 07:48:00

+0

一個問題將是在客戶端的socket.io的正確路徑,請使用螢火蟲檢查腳本是否已加載並調整/更改/ move_the_file,直到您將它加載到瀏覽器中'。是的,它在這裏工作就像一個魅力,我甚至進一步發送,並在客戶端和服務器端用setInterval發送消息。 – 2011-03-30 09:45:18

+0

感謝poelinca的工作....感謝您的幫助...你能告訴我任何教程節點js和套接字....... – 2011-03-31 04:35:05