29 lines
663 B
JavaScript
29 lines
663 B
JavaScript
|
const ws = new WebSocket('ws://localhost:8080');
|
||
|
|
||
|
ws.onopen = () => {
|
||
|
const userId = 'user123';
|
||
|
const token = generateToken(userId); // 使用相同密钥生成的JWT
|
||
|
|
||
|
ws.send(JSON.stringify({
|
||
|
type: 'auth',
|
||
|
userId,
|
||
|
token
|
||
|
}));
|
||
|
};
|
||
|
ws.send(JSON.stringify({
|
||
|
type: 'private_message',
|
||
|
receiverId: 'user456',
|
||
|
content: '你好!',
|
||
|
messageId: 'msg_123' // 客户端生成的唯一ID
|
||
|
}));
|
||
|
ws.send(JSON.stringify({
|
||
|
type: 'group_message',
|
||
|
groupId: 'group789',
|
||
|
content: '大家好!',
|
||
|
messageId: 'msg_456'
|
||
|
}));
|
||
|
ws.send(JSON.stringify({
|
||
|
type: 'withdraw_message',
|
||
|
messageId: 'msg_123'
|
||
|
}));
|
||
|
|