docs:更新WebSocket文档示例代码
- 将Socket.IO示例替换为原生WebSocket代码 - 更新JavaScript和Godot客户端示例 - 统一使用/game路径的WebSocket连接 - 简化示例代码,移除复杂的Godot逻辑
This commit is contained in:
@@ -47,7 +47,7 @@ export class WebSocketDocsController {
|
|||||||
properties: {
|
properties: {
|
||||||
url: {
|
url: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
example: 'ws://localhost:3000/game',
|
example: 'wss://whaletownend.xinghangee.icu/game',
|
||||||
description: 'WebSocket 连接地址'
|
description: 'WebSocket 连接地址'
|
||||||
},
|
},
|
||||||
namespace: {
|
namespace: {
|
||||||
@@ -92,8 +92,8 @@ export class WebSocketDocsController {
|
|||||||
getWebSocketDocs() {
|
getWebSocketDocs() {
|
||||||
return {
|
return {
|
||||||
connection: {
|
connection: {
|
||||||
url: 'ws://localhost:3000/game',
|
url: 'wss://whaletownend.xinghangee.icu/game',
|
||||||
namespace: '/game',
|
namespace: '/',
|
||||||
transports: ['websocket', 'polling'],
|
transports: ['websocket', 'polling'],
|
||||||
options: {
|
options: {
|
||||||
timeout: 20000,
|
timeout: 20000,
|
||||||
@@ -262,52 +262,52 @@ export class WebSocketDocsController {
|
|||||||
examples: {
|
examples: {
|
||||||
javascript: {
|
javascript: {
|
||||||
connection: `
|
connection: `
|
||||||
// 使用 Socket.IO 客户端连接
|
// 使用原生 WebSocket 客户端连接
|
||||||
const io = require('socket.io-client');
|
const ws = new WebSocket('wss://whaletownend.xinghangee.icu/game');
|
||||||
|
|
||||||
const socket = io('ws://localhost:3000/game', {
|
ws.onopen = function() {
|
||||||
transports: ['websocket', 'polling'],
|
console.log('连接成功');
|
||||||
timeout: 20000,
|
|
||||||
forceNew: true,
|
|
||||||
reconnection: true,
|
|
||||||
reconnectionAttempts: 3,
|
|
||||||
reconnectionDelay: 1000
|
|
||||||
});
|
|
||||||
|
|
||||||
// 连接成功
|
|
||||||
socket.on('connect', () => {
|
|
||||||
console.log('连接成功:', socket.id);
|
|
||||||
|
|
||||||
// 发送登录消息
|
// 发送登录消息
|
||||||
socket.emit('login', {
|
ws.send(JSON.stringify({
|
||||||
type: 'login',
|
type: 'login',
|
||||||
token: 'YOUR_JWT_TOKEN_HERE'
|
token: 'YOUR_JWT_TOKEN_HERE'
|
||||||
});
|
}));
|
||||||
});
|
};
|
||||||
|
|
||||||
// 登录成功
|
ws.onmessage = function(event) {
|
||||||
socket.on('login_success', (data) => {
|
const data = JSON.parse(event.data);
|
||||||
console.log('登录成功:', data);
|
console.log('收到消息:', data);
|
||||||
|
|
||||||
// 发送聊天消息
|
// 处理不同类型的消息
|
||||||
socket.emit('chat', {
|
if (data.t === 'login_success') {
|
||||||
t: 'chat',
|
console.log('登录成功:', data);
|
||||||
content: '大家好!',
|
|
||||||
scope: 'local'
|
// 发送聊天消息
|
||||||
});
|
ws.send(JSON.stringify({
|
||||||
});
|
t: 'chat',
|
||||||
|
content: '大家好!',
|
||||||
|
scope: 'local'
|
||||||
|
}));
|
||||||
|
} else if (data.t === 'chat_render') {
|
||||||
|
console.log('收到消息:', data.from, '说:', data.txt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 接收聊天消息
|
ws.onclose = function(event) {
|
||||||
socket.on('chat_render', (data) => {
|
console.log('连接关闭:', event.code, event.reason);
|
||||||
console.log('收到消息:', data.from, '说:', data.txt);
|
};
|
||||||
});
|
|
||||||
|
ws.onerror = function(error) {
|
||||||
|
console.error('连接错误:', error);
|
||||||
|
};
|
||||||
`,
|
`,
|
||||||
godot: `
|
godot: `
|
||||||
# Godot WebSocket 客户端示例
|
# Godot WebSocket 客户端示例
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
var socket = WebSocketClient.new()
|
var socket = WebSocketClient.new()
|
||||||
var url = "ws://localhost:3000/game"
|
var url = "wss://whaletownend.xinghangee.icu/game"
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
socket.connect("connection_closed", self, "_closed")
|
socket.connect("connection_closed", self, "_closed")
|
||||||
@@ -320,18 +320,18 @@ func _ready():
|
|||||||
print("连接失败")
|
print("连接失败")
|
||||||
|
|
||||||
func _connected(protocol):
|
func _connected(protocol):
|
||||||
print("WebSocket 连接成功")
|
print("连接成功")
|
||||||
# 发送登录消息
|
|
||||||
var login_msg = {
|
|
||||||
"type": "login",
|
|
||||||
"token": "YOUR_JWT_TOKEN_HERE"
|
|
||||||
}
|
|
||||||
socket.get_peer(1).put_packet(JSON.print(login_msg).to_utf8())
|
|
||||||
|
|
||||||
func _on_data():
|
func _on_data():
|
||||||
var packet = socket.get_peer(1).get_packet()
|
var packet = socket.get_peer(1).get_packet()
|
||||||
var message = JSON.parse(packet.get_string_from_utf8())
|
var message = JSON.parse(packet.get_string_from_utf8())
|
||||||
print("收到消息: ", message.result)
|
print("收到消息: ", message.result)
|
||||||
|
|
||||||
|
func _closed(was_clean_close):
|
||||||
|
print("连接关闭")
|
||||||
|
|
||||||
|
func _error():
|
||||||
|
print("连接错误")
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user