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