Socket.io 入门

设置一个简单的HTML网页,提供一个表单和一个信息的列表,通过node.js进行框架的搭建

使用node.js框架

1.新建一个chat-example文件夹,并建立一个package.json文件

{

  "name": "socket-chat-example",

  "version": "0.0.1",

  "description": "my first socket.io app",

  "dependencies": {}

}

2. 通过npm安装express,并建立index.js文件配置信息

npm install --save express@4.15.2
var app = require('express')();

var http = require('http').Server(app);

 

app.get('/', function(req, res){

  res.send('<h1>Hello world</h1>');

});

 

http.listen(3000, function(){

  console.log('listening on *:3000');

});

3.在命令行运行node index.js 你可以得到

 

 

在浏览器运行http://localhost:3000你可以得到

 

 

4.HTML页面的建立

创建index.html,并修改index.js里面的app.get函数,将路由连接到index.Html

<!doctype html>

<html>

  <head>

    <title>Socket.IO chat</title>

    <style>

      * { margin: 0; padding: 0; box-sizing: border-box; }

      body { font: 13px Helvetica, Arial; }

      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }

      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }

      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }

      #messages { list-style-type: none; margin: 0; padding: 0; }

      #messages li { padding: 5px 10px; }

      #messages li:nth-child(odd) { background: #eee; }

    </style>

  </head>

  <body>

    <ul id="messages"></ul>

    <form action="">

      <input id="m" autocomplete="off" /><button>Send</button>

    </form>

  </body>

</html>
app.get('/', function(req, res){

  res.sendFile(__dirname + '/index.html');

});

5.整合socket.Io

先安装socket.io

npm install --save socket.io

一般安装完,package.json会自动更新,再编辑index.js文件

var app = require('express')();

var http = require('http').Server(app);

var io = require('socket.io')(http);

 

app.get('/', function(req, res){

  res.sendFile(__dirname + '/index.html');

});

 

io.on('connection', function(socket){

  console.log('a user connected');

});

 

http.listen(3000, function(){

  console.log('listening on *:3000');

});

给index.html里面增加js代码,调取socket和jq框架,进行html和http的连接。

<script src="/socket.io/socket.io.js"></script>

<script src="https://code.jquery.com/jquery-1.11.1.js"></script>

<script>

  $(function () {

    var socket = io();

    $('form').submit(function(){

      socket.emit('chat message', $('#m').val());

      $('#m').val('');

      return false;

    });

  });

</script>

Index.js修改获取chat message节点,通过控制台查看message

io.on('connection', function(socket){

  socket.on('chat message', function(msg){

    console.log('message: ' + msg);

  });

});

通过io.emit进行事件发送

io.emit('some event', { for: 'everyone' });

简单起见,我们将发送消息给每个人,包括发送方。

io.on('connection', function(socket){

  socket.on('chat message', function(msg){

    io.emit('chat message', msg);

  });

});
  <script>

  $(function () {

    var socket = io();

    $('form').submit(function(){

      socket.emit('chat message', $('#m').val());

      $('#m').val('');

      return false;

    });

    socket.on('chat message', function(msg){

      $('#messages').append($('<li>').text(msg));

    });

  });

</script>

 

posted @ 2017-03-14 21:03  刘爽_杭州  阅读(245)  评论(0编辑  收藏  举报