首页 > 代码库 > nodejs学习(3) express+socket.io
nodejs学习(3) express+socket.io
//nodevar express=require(‘express‘);var app = express();var server = require(‘http‘).createServer(app);var io = require(‘socket.io‘)(server);app.use(express.static(‘public‘));io.on(‘connection‘, function(socket){ socket.on(‘message‘, function(msg){//接收 console.log(‘message: ‘ + msg); socket.emit(‘message‘, msg+"!!");//发送 });});server.listen(3000);
<!doctype html><html> <head> <title>socket.io test</title> </head> <body> <script src="/socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script> var socket = io(); socket.emit(‘message‘,"ping time:"+new Date().getTime()); socket.on(‘message‘, function(msg){ console.log(msg); }); </script> </body></html>
socket.io:https://github.com/Automattic/socket.io
socket.io-client:https://github.com/Automattic/socket.io-client
express版本:4.10.8
socket.io版本:1.2.1
nodejs学习(3) express+socket.io
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。