1.0.2 • Published 2 years ago
websockethttp v1.0.2
websockethttp-js
文档
使用
1. 引入依赖
npm install websockethttp
2. 导入模块
import client from 'websockethttp/client/index'
模块内部已经自动绑定实例到 window
if (window) {
window.$wsh = $wsh
}
所以引入代码可以这样写也行
import 'websockethttp/client/index'
然后通过window使用$wsh
window.$wsh.openConnection('ws://', null)
3. 编写代码
// 打开与服务器的连接
client.openConnection('ws://127.0.0.1:8080/websocket/http', () => {
console.log('连接成功')
// 送消息到服务器的 chat 处理器 下的 Room 方法 (服务器如果没有注册这个处理器则消息服务器不会处理)
client.sendTextMessage("Chat", "Room", "你好服务器", (response) => {
console.log("发送结果与服务器的返回结果", response)
})
// 客户端注册一个处理器,等待服务端的请求 名称是 Chat 路径是 Room
client.registerRequestHandlerFunction("Chat", "Room", (request, response) => {
console.log('服务器的数据', request)
// 响应结果给服务器
response.code = 0
response.msg = 'ok'
response.body = '消息收到'
})
})