<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script type="text/javascript">
var socket = io.connect("http://127.0.0.1:3001");
socket.on('news', function (data) {
console.log(data);
alert(data);
socket.emit('my other event', { my: 'data' });
});
</script>
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');
});
改进版 ---websocket与http共存--- to thi tha--
服务
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var g_socket; io.on('connection', function (socket) {
socket.emit('news', { hello: 'world1--lala' });
socket.on('my other event', function (data) {
console.log(data);
});
g_socket=socket;
});
// 服务器侦听在sockettest.com的3000端口上
http.listen(3001, function(){
//# 输出到标准输出
console.log(':3001');
}); var app1 = require('express')();
var http1 = require('http').Server(app1);
app1.get('/1', function(req, res){
g_socket.emit('news', { hello: '13:55world1--lala' });
});
http1.listen(80, function(){
//# 输出到标准输出
console.log(':80');
});
客户