socket.io立即启动和停止

时间:2013-08-04 21:35:42

标签: javascript node.js socket.io

我正在玩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>时会发生这种情况:

enter image description here

任何建议都表示赞赏!

1 个答案:

答案 0 :(得分:3)

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

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