js button onclick 传参_使用node.js如何简单快速的搭建一个websocket聊天应用

25ae399c2f9735a28481d8ce588c3a68.png
初始化项目
npm init
安装nodejs-websocket
npm install nodejs-websocket
创建并编辑启动文件

创建一个名为app.js文件,并且编辑它。

var ws = require("nodejs-websocket");
console.log("开始建立连接...")

var [user1,user2,user1Ready,user2Ready] = [null,null,false,false];

ws.createServer(function(conn){
    conn.on("text", function (str) {
        console.log("收到的信息为:"+str)
        if(str==="user1"){
            user1 = conn;
            user1Ready = true;
        }
        if(str==="user2"){
            user2 = conn;
            user2Ready = true;
        }
        if(user2Ready){
            user2.sendText(str);
        } 
        if(user1Ready){
          user1.sendText(str);
        }
    })
    conn.on("close", function (code, reason) {
        console.log("关闭连接")
    });
    conn.on("error", function (code, reason) {
        console.log("异常关闭")
    });
}).listen(8001)
console.log("WebSocket建立完毕")
分别创建并编辑两个用户页面文件

分别创建user1.htmluser2.html, 并且编辑它们。

「user1.html」

html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>user1title>
    <style>#mes{width: 300px;height: 300px;border: 1px solid #666;overflow: auto;margin-bottom:10px;padding: 5px;
        }li{text-decoration: none;background: #f4f4f4;padding: 5px;margin-top: 5px;
        }.tl{color: red;text-align: left;
        }.tr{color: green;text-align: right;
        }style>
head>
<body>
    <ul id="mes">ul>
    <div class="kuang">
        <input type="text" value="" class="int">
        <button class="send">发送button>
    div>
    <script>var mes = document.getElementById("mes");if(window.WebSocket){var ws = new WebSocket('ws://localhost:8001');
            ws.onopen = function(e){console.log("连接服务器成功");
                ws.send("user1");
            }
            ws.onmessage = function(e){if(e.data!=="user1"&&e.data!=="user2"){console.log(e);var newData = JSON.parse(e.data);var node=document.createElement("LI");var textnode=document.createTextNode(newData.mes);
                    node.appendChild(textnode);if(newData.name==="user2"){
                        node.classList.add("tl")
                        mes.appendChild(node);
                    } else{
                        node.classList.add("tr")
                        mes.appendChild(node);
                    }
                }document.querySelector(".send").onclick = function(e){var obj = {name:"user1",mes:document.querySelector(".int").value
                    }
                    ws.send(JSON.stringify(obj));document.querySelector(".int").value="";
                }
            }
            ws.onclose = function(e){console.log("服务器关闭");
            }
            ws.onerror = function(){console.log("连接出错");
            }
        }script>
body>
html>

「user2.html」

html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>user2title>
    <style>#mes{width: 300px;height: 300px;border: 1px solid #666;overflow: auto;margin-bottom:10px;padding: 5px;
        }li{text-decoration: none;background: #f4f4f4;padding: 5px;margin-top: 5px;
        }.tl{color: red;text-align: left;
        }.tr{color: green;text-align: right;
        }style>
head>
<body>
    <ul id="mes">ul>
    <div class="kuang">
      <input type="text" value="" class="int">
      <button class="send">发送button>
  div>
    <script>var mes = document.getElementById("mes");if(window.WebSocket){var ws = new WebSocket('ws://localhost:8001');
            ws.onopen = function(e){console.log("连接服务器成功");
                ws.send("user2");
            }
            ws.onmessage = function(e){if(e.data!=="user1"&&e.data!=="user2"){console.log(e);var newData = JSON.parse(e.data);var node=document.createElement("LI");var textnode=document.createTextNode(newData.mes);
                    node.appendChild(textnode);if(newData.name==="user1"){
                        node.classList.add("tl")
                        mes.appendChild(node);
                    } else{
                        node.classList.add("tr")
                        mes.appendChild(node);
                    }
                }document.querySelector(".send").onclick = function(e){var obj = {name:"user2",mes:document.querySelector(".int").value
                    }
                    ws.send(JSON.stringify(obj));document.querySelector(".int").value="";
                }
            }
            ws.onclose = function(e){console.log("服务器关闭");
            }
            ws.onerror = function(){console.log("连接出错");
            }
        }script>
body>
html>
启动项目
node app.js

作者:「Vam的金豆之路」

主要领域:「前端开发」

我的微信:「maomin9761」

微信公众号:「前端历劫之路」

ee133edbfa72463943234a2d0a24010e.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值