赞
踩
.chat-room { display: flex; flex-direction: column; height: 100%; .chat-room-set { display: flex; justify-content: space-between; align-items: center; width: 100%; font-size: 31rpx; font-weight: bold; .chat-room-name { padding: 10rpx; } .chat-room-logout { margin: 0; } } .chat-room-users { margin: 20rpx 0; padding: 20rpx 0; font-size: 28rpx; line-height: 1.5; border-bottom: 2rpx solid $e; word-break: break-all; } .chat-room-area { box-sizing: border-box; padding: 20rpx; height: calc(100% - 280rpx); background: $f8; .chat-room-area-item { display: flex; flex-direction: row; justify-content: flex-start; align-items: flex-start; margin-bottom: 20rpx; padding-bottom: 20rpx; .chat-room-user { .chat-room-username { color: $iptBorColor; font-size: 31rpx; line-height: 2; &.active { color: $mainColor; } } } .chat-room-avatar { width: 68rpx; height: 68rpx; background: $white; border-radius: 10rpx; } .chat-room-time { font-size: 26rpx; color: $iptBorColor; } .chat-room-content { box-sizing: border-box; margin-left: 12rpx; padding: 15rpx; max-width: calc(100% - 80rpx); text-align: left; font-size: 24rpx; color: $white; border-radius: 10rpx; word-break: break-all; background: $mainColor; } &.active { flex-direction: row-reverse; .chat-room-content { margin-left: 0; margin-right: 12rpx; text-align: right; color: $uni-text-color; background: $white; } } } } .chat-room-bot { position: fixed; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; box-sizing: border-box; padding: 0 30rpx; width: 100%; height: 100rpx; background: $white; border-top: 3rpx solid $f8; .chat-room-bot-ipt { flex: 1; box-sizing: border-box; padding: 0 30rpx; height: 70rpx; font-size: 24rpx; border: 2rpx solid $iptBorColor; border-radius: 10rpx; } .chat-room-bot-btn { margin-left: 50rpx; width: 150rpx; height: 70rpx; line-height: 70rpx; font-size: 26rpx; color: $white; background: $mainColor; } } }
// 连接ws function connectWs() { wsInfo.ws = null; wsInfo.ws = uni.connectSocket({ url: proxy.$apis.urls.wsUrl, success() { wsInfo.alive = true; console.log("ws连接成功!"); }, fail() { wsInfo.alive = false; console.log("ws连接失败!"); }, }); console.log("ws信息:", wsInfo.ws); // ws打开 wsInfo.ws.onOpen((res) => { wsInfo.alive = true; // 开启心跳 heartBeat(); console.log("ws开启成功!"); }); // ws消息 wsInfo.ws.onMessage((res) => { heartBeat(); // 处理消息 let data = JSON.parse(res.data); handlerMessage(data); console.log("ws接收消息:", data); }); // ws关闭 wsInfo.ws.onClose((res) => { wsInfo.alive = false; reConnect(); console.log("ws连接关闭:", res); }); // ws错误 wsInfo.ws.onError((err) => { wsInfo.alive = false; reConnect(); console.log("ws连接错误:", res); }); }
function handlerMessage(data) { let { code } = data; let { type } = data.data; // 用户登录 if (type == "login") { wsInfo.isLogin = code === 200; uni.showToast({ title: data.data.info, icon: code === 200 ? "success" : "error", }); } // 退出登录 if (code === 200 && type == "logout") { wsInfo.isLogin = false; userInfo.name = ""; uni.showToast({ title: "退出登录成功!", icon: "success", }); } // 加入房间成功 if (code === 200 && type == "join") { if (data.data.user.id == userInfo.id) { wsInfo.isJoin = true; } if (data.data.roomId == roomInfo.id) { let users = data.data.users, list = []; for (let item of users) { list.push(item.name); } userInfo.users = list; userInfo.usersText = list.join(","); if (data.data.user.id == userInfo.id) { msg.current = ""; msg.list = []; } } } // 加入房间失败 if (code === 101 && type == "join") { uni.showToast({ title: data.data.info, icon: "error", }); } // 离开房间 if (code === 200 && type == "leave") { if (data.data.user.id == userInfo.id) { wsInfo.isJoin = false; } if (data.data.roomId == roomInfo.id) { let users = data.data.users, list = []; for (let item of users) { list.push(item.name); } userInfo.users = list; userInfo.usersText = list.join(","); if (data.data.user.id == userInfo.id) { msg.current = ""; msg.list = []; } } } // 聊天内容 if (code === 200 && type == "chat") { if (data.data.roomId == roomInfo.id) { let list = data.data.msg; msg.list = list; roomInfo.goBottomTimer = setTimeout(() => { goBottom(); }, roomInfo.goBottomTime); } } }
// 用户登录 function userLogin(ws, info) { let ip = info.ip; let { id, name, avatar } = info.data, result = null; if (users.length >= MAX_USER_NUM) { result = { code: 101, msg: "system", data: { info: "服务器用户已满!", type: info.type, }, }; } else if (users.length === 0) { users.push({ id, name, avatar, ip, status: 1, // 1.在线 2.离线 roomId: 1, }); result = { code: 200, msg: "system", data: { info: "登录成功!", type: info.type, }, }; } else { let userInfo = users.find((s) => name === s.name); if (userInfo && userInfo.id) { let index = users.findIndex((s) => name == s.name); if (users[index].status === 2) { users[index].status = 1; result = { code: 200, msg: "system", data: { info: "登录成功!", type: info.type, }, }; } else { result = { code: 101, msg: "system", data: { info: "用户已登录!", type: info.type, }, }; } } else { users.push({ id, name, avatar, ip, status: 1, roomId: 1, }); result = { code: 200, msg: "system", data: { info: "登录成功!", type: info.type, }, }; } } returnMsg(ws, result); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。