我正在玩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>
时会发生这种情况:
任何建议都表示赞赏!
答案 0 :(得分:3)
您正在使用expressjs但未将其设置为侦听任何端口。
var app = express();
var server = http.createServer(app)
app.listen(8000);
var io = socket.listen(server);