var express = require('express')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(8888, function() {
console.log('listening on *:8888');
});
the front-end html file
<html>
<body>
<!-- add ur js file here, it server as static content from ur back-end service(express static middleware)-->
<!--<script src='/js/***.js'></script>-->
<script src='/socket.io/socket.io.js'></script>
</body>
</html>
websocket是基于http协议的。
好奇怪,为啥给的github地址是一个java-websocket的项目,问题却出现在了nodejs版块里面。。。我晕。 Anyway,我按照nodejs来说吧!现在是的!!!
the back-end js:
the front-end html file
socket.io会自动捕捉到
/socket.io
的请求,然后返回客户端文件给你。