nodejs学习(3) express+socket.io

时间:2021-06-23 12:38:33
//node
var 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>

nodejs学习(3) express+socket.io

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