小编这里的场景是小程序,前后端如何搭配才能接收到一个对象?
前端
发送时候要变成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() + "已上线");
}
}
标签:username,webSocket,getString,jsonObject,全栈,修养,user,online,type
From: https://blog.csdn.net/2401_82752568/article/details/136976825