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

posted on 2015-01-15 15:29  叫我钱了个浅  阅读(266)  评论(0编辑  收藏  举报

导航