我在玩 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>
时会发生这种情况:
任何建议表示赞赏!