封装保存用户类
utils/chat.js
class Chat{constructor(){// 当前登录的用户this._user = null;// 会话数组 和多个人this._sessions = []; //user message// 当前会话 (和谁在聊天)this._current_session = null;}setUser(user){this._user = user}
}export default new Chat()
main.js
app.config.globalProperties.$chat = chat;
login.vue
登录成功后跳转到home页
methods: {onSubmit() {if(!this.form.username){ElMessage.error("请输入用户名!")return}// 判断socket是否登录if(!this.$socket.connected){this.$socket.connect();}this.$socket.login(this.form.username,(result)=>{if(result['code']===200){const user = result['data'];this.$chat.setUser(user);this.$router.push({name:"home"});}else{ElMessage.error(result['message'])}});}
现在有个问题是把页面关闭之后再打开,输入之前登录过的用户名会提示用户名已存在
刷新页面或者断开连接 会把该用户删掉
@socketio.on("disconnect")
def disconnect():sid = request.sidfor use in online_users:if use["sid"] == sid:online_users.remove(use)
如果用户未登录,则跳转到登录页面