Socket.io 入门

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

使用node.js框架node

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

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

  "version": "0.0.1",

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

  "dependencies": {}

}

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

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 你能够获得npm

 

 

在浏览器运行http://localhost:3000你能够获得json

 

 

4.HTML页面的创建浏览器

建立index.html,并修改index.js里面的app.get函数,将路由链接到index.Htmlapp

<!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.iosocket

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>
相关文章
相关标签/搜索