簡體   English   中英

NodeJS + socket.io:簡單的客戶端/服務器示例不起作用

[英]NodeJS + socket.io: simple Client/Server example not working

我正在使用NodeJS v0.4.8和最新版本的socket.io

npm安裝socket.io

在Ubuntu上:

Linux mars 2.6.38-8-generic#42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU / Linux

不幸的是,以下代碼不會在客戶端或服務器端產生任何輸出。

有人知道嗎?

服務器端

var http = require('http'),  
io = require('socket.io'),
fs = require('fs'),
sys = require('sys');

respcont = fs.readFileSync('testclient.js');

server = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end(respcont);
});
server.listen(8082);

var socket = io.listen(server); 
socket.on('connection', function(client){ 

    sys.puts("New client is here!");
    client.send("hello world");

    client.on('message', function(msg) { sys.puts("client has sent:"+msg); }) ;
    client.on('disconnect', function() { sys.puts("Client has disconnected"); }) ;
}); 

客戶端

<html>
<body>
<script type="text/javascript" src="http://localhost:8082/socket.io/socket.io.js"></script>
<script> 
    var socket = new io.Socket(null,{port:8082,rememberTransport:true,timeout:1500});
    socket.connect();
    socket.on('connect', function() { 
        console.log('connected to server'); 
        socket.send('Hi Server...'); 
    });

    socket.on('message', function() { 
        console.log('received a message!');
    });

    socket.on('disconnect', function() { 
        console.log('disconnected from server'); 
    });

</script> 
</body>
</html>

NodeJS的輸出(不調用sys.puts(“ ...”))為:

信息-socket.io啟動調試-服務於靜態/socket.io.js調試-客戶端授權信息-握手授權信息-握手b61a5c2751c1c8c8493db4b79d19e779

Express 3.0 + Socket.io工作示例

服務器(app.js)

var express = require('express');
    var app = express.createServer();
    var socket = require('socket.io');
    app.configure(function(){
        app.use(express.static(__dirname + '/'));
    });
    var server = app.listen(8081);
    var io = socket.listen(server);
    io.sockets.on('connection', function (socket) {
        console.log("connnect");
        socket.on('disconnect', function (socket) {
        console.log("disconnect");
     });
}); 

客戶(index.html)

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost:8081');
</script>

您可以使用下面的鏈接派生代碼https://github.com/sreekumar-kr/Expree3.0---Socket.IO

我也像Derrish一樣喜歡使用表達框架來簡化我的工作( AWESOME :) 您可以從http://dl.dropbox.com/u/314941/socketio.zip下載並解壓縮此樣本。 我相信您甚至不必安裝這些模塊,因為感謝npm :),我已經將它們捆綁在本地( 只需運行 )。

如何安裝:

alfred@alfred-laptop:~/tmp/socketio$ uname -a
Linux alfred-laptop 2.6.35-28-generic #50-Ubuntu SMP Fri Mar 18 19:00:26 UTC 2011 i686 GNU/Linux
alfred@alfred-laptop:~/tmp$ wget http://dl.dropbox.com/u/314941/socketio.zip
alfred@alfred-laptop:~/tmp$ unzip socketio.zip
alfred@alfred-laptop:~/tmp$ cd socketio/
alfred@alfred-laptop:~/tmp/socketio$ node -v
v0.4.7
alfred@alfred-laptop:~/tmp/socketio$ npm -v
1.0.6
alfred@alfred-laptop:~/tmp/socketio$ node app.js

代碼:

app.js:

// npm install express
// npm install socket.io

var sys         = require('sys'),
        express = require('express'),
        app         = express.createServer('127.0.0.1'),
        io          = require('socket.io'); 

app.use(express.static(__dirname + '/public'));

app.get('/', function (req, res) {
    res.send('Hello World');
});

app.listen(3000);

var socket = io.listen(app); 

socket.on('connection', function (client){ 
  // new client is here!
  setTimeout(function () {
        client.send('Waited two seconds!');
    }, 2000);

  client.on('message', function () {
  }) ;

  client.on('disconnect', function () {
  });
});

public / index.html:

<html>
<p id="text">socket.io</p>

<script src="socket.io/socket.io.js"></script> 
<script src="jquery-1.6.1.min.js"></script><!-- Downloaded Jquery -->

<script> 
    $(document).ready(function(){

        var socket  = new io.Socket(),
                text        = $('#text');

        socket.connect();

        socket.on('connect', function () {
            text.html('connected');
        });

        socket.on('message', function (msg) {
            text.html(msg);
        });

        socket.on('disconnect', function () {
            text.html('disconnected');
        });

    });
</script> 

我的模塊清單:

alfred@alfred-laptop:~/tmp/socketio$ npm ls
/home/alfred/tmp/socketio
├─┬ express@2.3.11 
│ ├── connect@1.4.6 
│ ├── mime@1.2.2 
│ └── qs@0.1.0 
└── socket.io@0.6.18

已安裝的模塊(不需要):

npm install express
npm install socket.io

瀏覽器將顯示:

  1. 啟動時啟動socket.io ,但您可能看不到它,因為它將被connected代替。
  2. connected ,當用戶連接到socket.io。
  3. 2秒后,它將顯示Waited two seconds! 圖片

我以您的示例為例,並使用express將其放在了節點應用程序中。 您的HTML代碼被放置在public下的靜態HTML文件中。 您的示例工作正常。 代碼如下所示。 我想確保socket.io腳本文件和HTML文件均已正確提供。

var http = require('http'),  
    io = require('socket.io'),
    express = require('express');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

if (!module.parent) {
  app.listen(9000);
  console.log("server started at %s", (new Date()).toUTCString());
  console.log("listening on port %d", app.address().port);
}


// socket.io 
var socket = io.listen(app); 
socket.on('connection', function(client){ 
    console.log("New client is here!");
    client.send("hello world");
    client.on('message', function(msg){ console.log("client has sent:"+msg); }) ;
    client.on('disconnect', function(){ console.log("Client has disconnected"); }) ;
    client.on('disconnect', function(){ }) 
 }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM