码迷,mamicode.com
首页 > Web开发 > 详细

nodejs学习(3) express+socket.io

时间:2015-01-15 17:35:35      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

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

nodejs学习(3) express+socket.io

标签:

原文地址:http://www.cnblogs.com/qianlegeqian/p/4226441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!