https://blog.csdn.net/qq_30100043/article/details/53005478
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> <script type="text/javascript"> // var socket = io.connect(‘ws://127.0.0.1:3001‘); var socket = io.connect("//127.0.0.1:3001"); socket.on(‘news‘, function (data) { console.log(data); alert(data); socket.emit(‘my other event‘, { my: ‘data‘ }); }); </script>
var app = require(‘express‘)();
var http = require(‘http‘).Server(app);
var io = require(‘socket.io‘)(http);
var fs = require(‘fs‘);
app.use(function(req, res, next){
res.io = io;
next();
});
//#默认打开文件
app.get(‘/‘, function(req, res){
res.sendfile(‘./index.html‘);
});
io.on(‘connection‘, function (socket) {
socket.emit(‘news‘, { hello: ‘world1--lala‘ });
socket.on(‘my other event‘, function (data) {
console.log(data);
});
});
// 服务器侦听在sockettest.com的3000端口上
http.listen(3001, function(){
//# 输出到标准输出
console.log(‘:3001‘);
});