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
的請求,然後回傳客戶端檔案給你。