设为首页 加入收藏

TOP

nodejs实现聊天机器人(二)
2019-09-19 18:10:06 】 浏览:120
Tags:nodejs 实现 聊天 机器人
ate(-50%, -50%)
; background-color: #eee; width: 320px; height: 564px; box-sizing: border-box; } .title { height: 40px; line-height: 40px; text-align: center; background-color: #000; color: #fff; position: relative; font-size: 16px; } .input-box { margin-top: 10px; position: absolute; bottom: 0; background-color: #fff; width: 100%; height: 40px; line-height: 32px; padding: 4px; padding-right: 0; box-sizing: border-box; display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; -ms-align-items: center; align-items: center; justify-content: space-between; border-top: 1px solid #eee; } .input { vertical-align: top; height: 32px; line-height: 32px; outline: none; border: 1px solid #ccc; padding: 0 4px; box-sizing: border-box; flex: 1; background-color: #eee; border-radius: 4px; margin-right: 10px; margin-left: 4px; } .input:focus { border: 1px solid #ccc; } .send { width: 80px; text-align: center; height: 32px; line-height: 32px; cursor: pointer; background-color: green; color: #fff; margin-right: 10px; font-size: 14px; } .send:active { opacity: 0.6; } li { list-style: none; padding: 6px 10px; box-sizing: border-box; } .my-say { text-align: right; } .say { display: inline-block; background-color: #fff; font-size: 12px; padding: 6px 4px; border-radius: 4px; margin-top: 1px; vertical-align: top; max-width: 220px; } .computer-say .sayman { background-color: #40E0D0; } .my-say .sayman { background-color: #FFA500; } .my-say .say { text-align: left; } .sayman { font-size: 10px; display: inline-block; height: 30px; width: 30px; background-color: #ccc; border-radius: 50%; text-align: center; line-height: 30px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 4px; box-sizing: border-box; margin: 0 4px; color: #fff; } .view { position: absolute; top: 40px; bottom: 40px; left: 0; width: 100%; padding: 10px 0; box-sizing: border-box; overflow-y: auto; }

 

8.在bbs文件夹中创建index.js文件,作为客户端js处理文件。

内容如下:

let submit = document.getElementById("submit"),
    pl = document.getElementById("pl");
// 很重要 必须写,判断浏览器是否支持websocket
let CreateWebSocket = (() => {
    return (urlValue) => {
        if (window.WebSocket) return new WebSocket(urlValue);
        if (window.MozWebSocket) return new MozWebSocket(urlValue);
        return false;
    }
})()
// 实例化websoscket websocket有两种协议ws(不加密)和wss(加密)
let webSocket = CreateWebSocket(`ws://127.0.0.1:3000`);
webSocket.onopen = evt => {
    addMsg(1, '你好,欢迎进入实时聊天室!')
}
webSocket.onmessage = evt => {
    // 这是服务端返回的数据
    addMsg(1, evt.data);
    submit.innerHTML = '发送';
}
// input事件发送数据
submit.onclick = (e) => {
    if (e.target.innerHTML == '回复中...') {
        return false
    }
    e.target.innerHTML = '回复中...';
    const str = document.getElementById("pl").value;
    webSocket.send(str);
    addMsg(2, str);
}
// 绑定回车事件
function keyEnter() {
    if (event.keyCode == 13) {
        document.getElementById("submit").click();
    }
}
// 添加消息
function addMsg(type, msg) {
    let li = document.createElement('li');
    // 1机器人/2自己
    if (typ
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇好看的鼠标hover效果 下一篇vue-Element-axios搭建调用api进..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目