• websocket+node建立聊天室简单使用


    1.建立新的文件夹dome

    2.执行 npm init加载package.json文件

    3.node不支持websocket所以npm install  ws 下载 ws插件

    4.建立index.html文件代码如下

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>WebSocket Chat</title>
    </head>

    <body>
    <script type="text/javascript">
      var socket;
      if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
      }
      if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:8090/ws");
        socket.onmessage = function(event) {
          var ta = document.getElementById("responseText");
          ta.value = ta.value + " " + event.data;
        };
         socket.onopen = function(event) {
          var ta = document.getElementById("responseText");
          ta.value = "连接开启!";
        };
        socket.onclose = function(event) {
          var ta = document.getElementById("responseText");
          ta.value = ta.value + "连接被关闭";
        };
      } else {
        alert("你的浏览器不支持 WebSocket!");
      }

      function send(message) {
        if (!window.WebSocket) {
          return;
        }
        if (socket.readyState == WebSocket.OPEN) {
          socket.send(message);
        } else {
          alert("连接没有开启.");
        }
      }
    </script>
    <form onsubmit="return false;">
      <h3>WebSocket 聊天室:</h3>
      <textarea id="responseText" style=" 500px; height: 300px;"></textarea>
      <br />
      <input type="text" name="message" style=" 300px" value="Welcome to waylau.com" />
          <input type="button" value="发送消息" onclick="send(this.form.message.value)" />
      <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空聊天记录" />
    </form>
    </body>
    </html>
     
    5.建立index.js文件代码如下
     
    const WebSocket = require("ws");

    const server = new WebSocket.Server({ port: 8090 });

    server.on("open", function open() {
    console.log("connected");
    });

    server.on("close", function close() {
    console.log("disconnected");
    });

    server.on("connection", function connection(ws, req) {
    const ip = req.connection.remoteAddress;
    const port = req.connection.remotePort;
    const clientName = ip + port;

    console.log("%s is connected", clientName);

    // 发送欢迎信息给客户端

    ws.send("Welcome " + clientName);

    ws.on("message", function incoming(message) {
    console.log("received: %s from %s", message, clientName);

    // 广播消息给所有客户端
    server.clients.forEach(function each(client) {
         if (client.readyState === WebSocket.OPEN) {
        client.send(clientName + " -> " + message);
      }
    });
    });
    });
     
    6.启动代码  node index.js
     
    7.在浏览器打开index.html 就可以查看了
     
  • 相关阅读:
    oracle job
    mysql与oracle之间的数据类型转换
    Oracle ORA-02069: 此操作的 global_names 参数必须设置为 TRUE
    oracle sequence 详解
    ORA-08004: 序列 SEQ_XXX.NEXTVAL exceeds MAXVALUE 无法实例化
    How to change Hostname / IP for a Grid Infrastructure Oracle Restart Standalone Configuration (SIHA) (文档 ID 1552810.1)
    oracle-1条updata的故事
    ADAPTIVE LOG FILE SYNC 引起的高Log File Sync警示
    oracle 静默创建数据库
    oracle 中文Linux安装乱码问题
  • 原文地址:https://www.cnblogs.com/cnlg123/p/11940881.html
Copyright © 2020-2023  润新知