# lws **Repository Path**: linke0/lws ## Basic Information - **Project Name**: lws - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-08-24 - **Last Updated**: 2021-08-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LWS webmsg 项目的 js 客户端 ## 用法 以 vue 项目作为演示,其他类似,不需要关注`getQueryString`、`ajax`、`formatParams`这些方法,重点关注其他的 ```js import LWS from "lsp-ws"; export default { name: "LWS", data() { return { topMessage: "", normalMessage: "", logContent: "", roomId: "", userId: "", broadcastMessage: "", target: "", targetType: "room", targetContent: "", conn: null, }; }, mounted() { this.loaded(); }, methods: { getQueryString(name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); let r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; }, //创建ajax函数 ajax(options) { options = options || {}; options.type = (options.type || "GET").toUpperCase(); options.dataType = options.dataType || "json"; let params = this.formatParams(options.data, options.dataType); //创建-第一步 let xhr; xhr = new XMLHttpRequest(); //接收-第三步 xhr.onreadystatechange = function () { if (xhr.readyState === 4) { let status = xhr.status; if (status >= 200 && status < 300) { options.success && options.success(xhr.responseText, xhr.responseXML); } else { options.error && options.error(status); } } }; //连接和发送-第二步 if (options.type === "GET") { xhr.open("GET", options.url + "?" + params, true); xhr.send(null); } else if (options.type === "POST") { xhr.open("POST", options.url, true); //设置表单提交时的内容类型 if (options.dataType === "json") { xhr.setRequestHeader("Content-Type", "application/json"); } else { xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ); } xhr.send(params); } }, formatParams(data, dataType) { if (dataType === "json") { return JSON.stringify(data); } else { let arr = []; for (let name in data) { arr.push( encodeURIComponent(name) + "=" + encodeURIComponent(data[name]) ); } arr.push(("v=" + Math.random()).replace(".", "")); return arr.join("&"); } }, normalSend() { // 发送普通消息,目前只能发送ping字符串 // this.conn.ws对象为原生js Websocket对象 this.conn.ws.send(this.normalMessage); }, disconnect() { // 断开连接 this.conn.close(); }, joinRoom() { // 加入房间 this.conn.join(this.roomId); }, leaveRoom() { // 离开房间 this.conn.leave(this.roomId); }, register() { // 自注册身份标识 this.conn.register(this.userId); }, sendToTarget() { // 点对点发送消息 this.conn.send(this.target, this.targetType, this.targetContent); }, broadcast() { // 发送广播 this.conn.broadcast(this.broadcastMessage); }, apiSend() { // api推送示例,后端使用,前端基本不用 this.ajax({ url: "http://127.0.0.1:8080/push", type: "POST", dataType: "json", data: { to: this.target, type: this.targetType, content: this.targetContent, }, success: (response, xml) => { this.topMessage = JSON.stringify(response) + JSON.stringify(xml); }, error: (status) => { this.topMessage = JSON.stringify(status); }, }); }, loaded() { let schema = "ws"; if (this.getQueryString("wss")) { schema = "wss"; } try { // 创建LWS对象示实例 this.conn = LWS({ url: schema + "://127.0.0.1:8080", // 设置open事件处理器 open: (ev) => { console.log("[open]", ev); }, // 设置close事件处理器 close: (ev) => { console.log("[close]", ev); this.logContent += "