nodejs学习(3) express+socket.io

//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>


技术分享

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。