我在哪里可以找到结合 express 3.0 的 socket.io 1.0 的工作示例?/socket.io/socket.io.js
目前的问题是它在我的 index.html 中找不到 。
var express = require('express')
var http = require('http')
var app = express()
app.configure(function () {
app.set('port', process.env.PORT || 80);
});
// setup express server
var serv = http.Server(app);
// setup socket io
var io = require('socket.io')(serv);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
serv.listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
这就是我在 index.html 中的内容
<!DOCTYPE html>
<html lang="en">
<head>
<title>Socket Example</title>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
<b>Socket IO</b>
</body>
这就是我在 package.json 中的内容
{
"name": "socketio",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.0",
"socket.io": "LearnBoost/socket.io"
},
"scripts": {
"start": "node app.js"
}
}