zoukankan      html  css  js  c++  java
  • node socket.io web

    soket.io & web

    http://socket.io/get-started/chat/

    • 新建一個文件夾 soketWeb ;
    • 在sokertWeb 文件夾內新建一個 package.json 文件;內容如下:
    {
      "name": "socketWebExample",
      "version": "0.0.1",
      "description": "it is my first app",
      "dependencies": {
        "socket.io": "^1.3.5"
      }
    }
    
    
    • 在控制檯中 進到soketWeb 文件夾中 安裝依賴
    npm install --save express@4.10.2
    
    • 新建一個index.js 文件;內容如下:
    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');
    });
    
    • 在控制檯中 啟動服務器;運行命令
    node index.js
    
    • 在瀏覽器中輸入 http://localhost:3000/ 將看到hello world; 表示運行成功;
    • 修改index.js 的代碼,添加如下:
    app.get('/',function (req,res) {
        //res.send('<h1>hello world</h1>');
        res.sendFile(__dirname+'/index.html');
    });
    
    • 新建一個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;  100%; }
          form input { border: 0; padding: 10px;  90%; margin-right: .5%; }
          form button {  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>
         <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
        <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
      </body>
    </html>
    
    • 在控制檯中 進到soketWeb 文件夾中 安裝依賴
     npm install --save socket.io
    
    • 再修改index.js 內容如下:
    var app=require('express')();
    var http=require('http').Server(app);
    var io=require('socket.io')(http);
    app.get('/',function (req,res) {
        //res.send('<h1>hello world</h1>');
        res.sendFile(__dirname+'/index.html');
    });
    io.on('connection',function(socket){
        console.log('has a user connected');
        socket.on('disconnect', function(){
        console.log('user disconnected');
      });
    });
    http.listen(3000,function(){
        console.log('listening on :3000');
    });
    
    • 在index.html 中添加一下的代碼:
        <script>
        var socket = io();
      </script>
    
    • 再一次在瀏覽器中運行 http://localhost:3000/ 如圖;
      好 連接成功!!!
    • 又一次修改 index.html 向服務器 發送消息;
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
    </script> 
    
    • 修改index.js 服務器這邊的接收到信息後處理;
    io.on('connection',function(socket){
        socket.on('chat message', function(msg){
        console.log('message: ' + msg);
      });
    });
    

    OK 到這裏就差不多了 ;
    可以開始通話了;

    • 再修改index.js 廣播接收到的信息;
    var app=require('express')();
    var http=require('http').Server(app);
    var io=require('socket.io')(http);
    app.get('/',function (req,res) {
        //res.send('<h1>hello world</h1>');
        res.sendFile(__dirname+'/index.html');
    });
    io.on('connection',function(socket){
        console.log('has a user connected');
            socket.on('disconnect', function(){
            console.log('user disconnected');
          });
    socket.on('chat message', function(msg){
        io.emit('chat message', msg);
      });
    });
    http.listen(3000,function(){
        console.log('listening on :3000');
    });
    
    • 在index.html 中修改 當接收到信息時 ,添加到聊天記錄裏面 顯示處理;
        <script>
      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>
    
  • 相关阅读:
    计算 sql查询语句所花时间
    iframe自适应高度,以及一个页面加载多个iframe
    窗体移动API和窗体阴影API
    js复习:
    web组合查询:
    web登陆,增删改加分页。
    cookie和Session传值
    控件及其数据传输
    ASP.NET WebForm
    三月总结
  • 原文地址:https://www.cnblogs.com/xieyier/p/4459757.html
Copyright © 2011-2022 走看看