WebSocket 基础信息(前端部分)
WebSocket 状态
open
close
error
message
connection
前端常用的事件处理函数
open
close
error
message
后端常用的事件处理函数
open
close
error
message
connection
案例(原生 JS)
前端环境环境
1 | npm install -g yarn |
package.json 添加脚本命令
1 | { |
前端代码
enter 页面 即 登录页面
1 | <!DOCTYPE html> |
entry 页面对应的 js 文件
1 | ;((doc, storage, location) => { |
index 页面
1 | <!DOCTYPE html> |
index 页面对应的 js 文件 (主要是这边配置了 WebSocket )
1 | ;((doc,socket, storage,location) => { |
配置 ws 后端服务
1 | mkdir server |
配置启动命令 (使用 nodemon 去监控 index.js 文件)
1 | { |
index.js ws 服务后端代码
1 | const Ws = require('ws'); |