前端
发送时候要变成json字符串
function sendOnLogin(online) {
const params = {
type: 'login',
username: username,
online: online
}
wx.sendSocketMessage({
data: JSON.stringify(params)
})
}
后端
@OnMessage
public void onMessage(String message, Session session) throws IOException {
JSONObject jsonObject = JSON.parseObject(message);
String type = jsonObject.getString("type");
if(type.equals("login")){
this.username = jsonObject.getString("username");
webSocketSet.add(this);
User user = new User();
user.setUsername(jsonObject.getString("username"));
user.setOnline(jsonObject.getString("online"));
loginMapper.updateUser(user);
log.info(user.getUsername() + "已上线");
}
}