0

我在玩 node.js 和 socket.io。我必须归档:

服务器端:

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

var app = express();
var server = http.createServer(app)
var io = socket.listen(server);

io.sockets.on('connect', function (client) {
    console.log('Client connected');
    client.emit('messages', {hello: "hello"});
});

客户端:

<script src="/socket.io/socket.io.js"></script>

<script>
var server = io.connect('http://127.0.0.1:8000');
server.on('messages', function(data){
    alert(data.hello);
});
</script>

当我键入node <name-of-my-file>时会发生这种情况:

在此处输入图像描述

任何建议表示赞赏!

4

1 回答 1

3

您正在使用 expressjs,但您没有将其设置为侦听任何端口。

var app = express();
var server = http.createServer(app)
app.listen(8000);
var io = socket.listen(server);
于 2013-08-04T21:46:59.590 回答