使用koa和socket.io簡單搭建多人聊天流程

koa與socket.io簡單流程分析:html

1. 服務端觸發初始化io.on('connection', socket => {});
2. 客戶端發送say會話socket.emit('say', '我是客戶端'); 
3. 服務端監聽say會話socket.on('say',data => {}); 
4. 服務端發送message會話socket.emit('message', {hello: '你是誰'});
5. 客戶端接收message消息socket.on('message', (data) => {});

服務端:app

const koa = require('koa');
const app = new koa();
const server = require('http').Server(app.callback())
const io = require('socket.io')(server);
const port = 8081;

server.listen(process.env.PORT || port, () => {
    console.log(`app run at : http://127.0.0.1:${port}`);
})

io.on('connection', socket => {
    console.log('socket初始化完成');
    socket.on('say', data => {
        console.log(data, '接收到的信息')
        socket.emit('message', {hello: '你是誰'})
    })
})

客戶端koa

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    input {
      background-color: #fff;
      background-image: none;
      border-radius: 4px;
      border: 1px solid #bfcbd9;
      box-sizing: border-box;
      color: #1f2d3d;
      font-size: inherit;
      height: 40px;
      line-height: 1;
      outline: 0;
      padding: 3px 10px;
    }
    .el-button--primary {
      color: #fff;
      background-color: #20a0ff;
      border-color: #20a0ff;
    }
    .el-button {
      display: inline-block;
      line-height: 1;
      white-space: nowrap;
      cursor: pointer;
      background: #00aac5;
      border: 1px solid #c4c4c4;
      color: #fff;
      margin: 0;
      padding: 10px 15px;
      border-radius: 4px;
      outline: 0;
      text-align: center;
    }
  </style>
</head>
<body>
  <div>
    <div id="content">
    </div>
  </div>
  <div>
    <input type="text" id="input">
    <button class="el-button el-button--primary el-button--large" type="button" onclick="say()"><span>發送消息</span></button>
  </div>
  <script src="./socket.io.js"></script>
  <script>
    // 創建鏈接
    var socket = io.connect('http://localhost:8081');
    // 監聽 message 會話
    socket.on('message', function (data) {
      let html = document.createElement('p')
      html.innerHTML = `系統消息:<span>${data.hello}</span>`
      document.getElementById('content').appendChild(html)
      console.log(data);
    });
    // 按鈕點擊事件
    function say() {
      let t = document.getElementById('input').value
      if (!t) return
      let html = document.createElement('p')
      html.innerHTML = `你細聲說:<span>${t}</span>`
      document.getElementById('content').appendChild(html)
      socket.emit('say', { my: t});
    }
    // 監聽 news 會話
    socket.on('news', function (data) {
      console.log(data);
      let html = document.createElement('p')
      html.innerHTML = `小皮咖說:<span>我知道了,你說「${data.hello}」</span>`
      document.getElementById('content').appendChild(html)
    });
    // 監聽吃飯會話
    socket.on('eating', function (data) {
      console.log(data);
      let html = document.createElement('p')
      html.innerHTML = `小皮咖說:${data.hello}`
      document.getElementById('content').appendChild(html)
    });
  </script>
</body>
</html>
相關文章
相關標籤/搜索