Compare commits

...

3 Commits

Author SHA1 Message Date
杨号敬
469117ff7b q 2025-01-06 11:58:55 +08:00
杨号敬
6c617e8e5b Merge branch 'main' of http://shu.sheziwanglo.cn:3000/shurongsen/Cute_demon_attacks 2025-01-06 11:58:06 +08:00
杨号敬
bbc158ec6e 钓手接口 2025-01-06 11:57:49 +08:00
8 changed files with 603 additions and 0 deletions

View File

@ -101102,6 +101102,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1073200880}
- component: {fileID: 1073200881}
m_Layer: 5
m_Name: The_best_fisherman
m_TagString: Untagged
@ -101130,6 +101131,19 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1073200881
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1073200879}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ec0bc6b6d3f7fd044847aebc4ea31592, type: 3}
m_Name:
m_EditorClassIdentifier:
count: 0
--- !u!1 &1073869858
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bcbd296c0613aa345925c1068e00107c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json;
public class WebBeton : MonoBehaviour
{
private ClientWebSocket _webSocket;
private const string WebSocketUri = "ws://47.95.201.243:9527/api/ws";
private const string AuthorizationValue = "Bearer VH8XwWuu_iVjEPYQmCq1E2rITTg";
public int count = 0;
private async void Start()
{
await ConnectWebSocket();
// 准备 JSON 数据
var message = new
{
code = "FISHING",
content = "{\"action\":\"BETTING\",\"count\":1}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonUtility.ToJson(message);
// 调用发送方法
await SendJsonMessage(count);
}
private async Task ConnectWebSocket()
{
_webSocket = new ClientWebSocket();
// 添加 Authorization 头
_webSocket.Options.SetRequestHeader("Authorization", AuthorizationValue);
try
{
Debug.Log("正在连接到 WebSocket...");
await _webSocket.ConnectAsync(new Uri(WebSocketUri), CancellationToken.None);
Debug.Log("WebSocket 连接成功!");
// 开始接收消息
_ = ReceiveMessages();
}
catch (Exception e)
{
Debug.LogError($"WebSocket 连接失败:{e.Message}");
}
}
private async Task ReceiveMessages()
{
var buffer = new byte[1024];
try
{
while (_webSocket.State == WebSocketState.Open)
{
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
Debug.Log("WebSocket 连接已被服务器关闭。");
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "关闭连接", CancellationToken.None);
}
else
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Debug.Log($"接收到消息:{message}");
// 解析消息为 Fishresponse 对象
FishBetonresponse fishResponse = JsonConvert.DeserializeObject<FishBetonresponse>(message);
Promptmgr.Instance.PromptBubble(fishResponse.message);
// 检查是否成功反序列化
if (fishResponse != null && fishResponse.data != null)
{
Debug.Log($"余额: {fishResponse.data.balance}");
Debug.Log($"价格: {fishResponse.data.price}");
Debug.Log($"剩余数量: {fishResponse.data.surplus_count}");
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
}
}
}
catch (Exception e)
{
Debug.LogError($"接收消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(int count)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var message = new
{
code = "FISHING",
content = $"{{\"action\":\"BETTING\",\"count\":{count}}}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonConvert.SerializeObject(message);
var encodedMessage = Encoding.UTF8.GetBytes(jsonMessage);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{jsonMessage}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
private async void OnApplicationQuit()
{
if (_webSocket != null && _webSocket.State == WebSocketState.Open)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "应用程序退出", CancellationToken.None);
_webSocket.Dispose();
Debug.Log("WebSocket 连接已关闭。");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e7c2d58554fa36479761c2cbf8e106b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json;
using System.Drawing;
public class WebFishing : MonoBehaviour
{
private ClientWebSocket _webSocket;
private const string WebSocketUri = "ws://47.95.201.243:9527/api/ws";
private const string AuthorizationValue = "Bearer VH8XwWuu_iVjEPYQmCq1E2rITTg";
private async void Start()
{
await ConnectWebSocket();
// 准备 JSON 数据
var message = new
{
code = "FISHING",
content = "{\"action\":\"FISHING\"}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonUtility.ToJson(message);
// 调用发送方法
await SendJsonMessage("{ \"code\": \"FISHING\", \"content\": \"{\\\"action\\\":\\\"FISHING\\\"}\" }");
}
private async Task ConnectWebSocket()
{
_webSocket = new ClientWebSocket();
// 添加 Authorization 头
_webSocket.Options.SetRequestHeader("Authorization", AuthorizationValue);
try
{
Debug.Log("正在连接到 WebSocket...");
await _webSocket.ConnectAsync(new Uri(WebSocketUri), CancellationToken.None);
Debug.Log("WebSocket 连接成功!");
// 开始接收消息
_ = ReceiveMessages();
}
catch (Exception e)
{
Debug.LogError($"WebSocket 连接失败:{e.Message}");
}
}
private async Task ReceiveMessages()
{
var buffer = new byte[1024];
try
{
while (_webSocket.State == WebSocketState.Open)
{
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
Debug.Log("WebSocket 连接已被服务器关闭。");
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "关闭连接", CancellationToken.None);
}
else
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Debug.Log($"接收到消息:{message}");
// 解析消息为 Fishresponse 对象
Fishingresponse fishResponse = JsonConvert.DeserializeObject<Fishingresponse>(message);
Promptmgr.Instance.PromptBubble(fishResponse.message);
// 检查是否成功反序列化
if (fishResponse != null && fishResponse.data != null)
{
Debug.Log($"余额: {fishResponse.data.balance}");
Debug.Log($"价格: {fishResponse.data.price}");
Debug.Log($"剩余数量: {fishResponse.data.surplus_count}");
Debug.Log($"余额: {fishResponse.data.prize.id}");
Debug.Log($"余额: {fishResponse.data.prize.name}");
Debug.Log($"余额: {fishResponse.data.prize.type}");
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
}
}
}
catch (Exception e)
{
Debug.LogError($"接收消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(string json)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var encodedMessage = Encoding.UTF8.GetBytes(json);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{json}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
private async void OnApplicationQuit()
{
if (_webSocket != null && _webSocket.State == WebSocketState.Open)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "应用程序退出", CancellationToken.None);
_webSocket.Dispose();
Debug.Log("WebSocket 连接已关闭。");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 29b5ece7ff1f9c946aee00668809f293
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,270 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json;
public class WebJoinroom : MonoBehaviour
{
private ClientWebSocket _webSocket;
private const string WebSocketUri = "ws://47.95.201.243:9527/api/ws";
private const string AuthorizationValue = "Bearer VH8XwWuu_iVjEPYQmCq1E2rITTg";
public int count = 0;
private async void Start()
{
await ConnectWebSocket();
// 准备 JSON 数据
var message = new
{
code = "FISHING",
content = "{\\\"action\\\":\\\"INFO\\\"}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonUtility.ToJson(message);
// 调用发送方法
await SendJsonMessage("{ \"code\": \"FISHING\", \"content\": \"{\\\"action\\\":\\\"INFO\\\"}\" }");
await SendJsonMessage(count);
await SendJsonMessage("{ \"code\": \"FISHING\", \"content\": \"{\\\"action\\\":\\\"FISHING\\\"}\" }");
}
private async Task ConnectWebSocket()
{
_webSocket = new ClientWebSocket();
// 添加 Authorization 头
_webSocket.Options.SetRequestHeader("Authorization", AuthorizationValue);
try
{
Debug.Log("正在连接到 WebSocket...");
await _webSocket.ConnectAsync(new Uri(WebSocketUri), CancellationToken.None);
Debug.Log("WebSocket 连接成功!");
// 开始接收消息
_ = ReceiveMessages();
}
catch (Exception e)
{
Debug.LogError($"WebSocket 连接失败:{e.Message}");
}
}
private async Task ReceiveMessages()
{
var buffer = new byte[1024];
try
{
while (_webSocket.State == WebSocketState.Open)
{
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
Debug.Log("WebSocket 连接已被服务器关闭。");
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "关闭连接", CancellationToken.None);
}
else
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Debug.Log($"接收到消息:{message}");
var baseResponse = JsonConvert.DeserializeObject<BaseResponse>(message);
if (baseResponse != null)
{
switch (baseResponse.code)
{
// 解析消息为 Fishresponse 对象
case "FISHING_INFO":
// 解析为 FishJoinroomresponse 类型
FishJoinroomresponse fishResponse = JsonConvert.DeserializeObject<FishJoinroomresponse>(message);
if (fishResponse?.data != null)
{
Debug.Log($"余额: {fishResponse.data.balance}");
Debug.Log($"价格: {fishResponse.data.price}");
Debug.Log($"剩余数量: {fishResponse.data.surplus_count}");
Debug.Log($"玩法介绍: {fishResponse.data.intro_text}");
foreach (var item in fishResponse.data.items)
{
Debug.Log($"物品ID: {item.id}, 物品名称: {item.name}, 类型: {item.type}");
}
}
break;
case "FISHING_BETTING":
// 解析消息为 Fishresponse 对象
FishBetonresponse fishResponse1 = JsonConvert.DeserializeObject<FishBetonresponse>(message);
Promptmgr.Instance.PromptBubble(fishResponse1.message);
// 检查是否成功反序列化
if (fishResponse1 != null && fishResponse1.data != null)
{
Debug.Log($"余额: {fishResponse1.data.balance}");
Debug.Log($"价格: {fishResponse1.data.price}");
Debug.Log($"剩余数量: {fishResponse1.data.surplus_count}");
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
case "FISHING_FISHING":
// 解析消息为 Fishresponse 对象
Fishingresponse fishResponse2 = JsonConvert.DeserializeObject<Fishingresponse>(message);
Promptmgr.Instance.PromptBubble(fishResponse2.message);
// 检查是否成功反序列化
if (fishResponse2 != null && fishResponse2.data != null)
{
Debug.Log($"余额: {fishResponse2.data.balance}");
Debug.Log($"价格: {fishResponse2.data.price}");
Debug.Log($"剩余数量: {fishResponse2.data.surplus_count}");
Debug.Log($"余额: {fishResponse2.data.prize.id}");
Debug.Log($"余额: {fishResponse2.data.prize.name}");
Debug.Log($"余额: {fishResponse2.data.prize.type}");
}
else
{
Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
}
break;
}
}
}
}
}
catch (Exception e)
{
Debug.LogError($"接收消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(int count)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var message = new
{
code = "FISHING",
content = $"{{\"action\":\"BETTING\",\"count\":{count}}}"
};
// 将对象序列化为 JSON 字符串
string jsonMessage = JsonConvert.SerializeObject(message);
var encodedMessage = Encoding.UTF8.GetBytes(jsonMessage);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{jsonMessage}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
public async Task SendJsonMessage(string json)
{
if (_webSocket == null || _webSocket.State != WebSocketState.Open)
{
Debug.LogError("WebSocket 未连接,无法发送消息。");
return;
}
try
{
var encodedMessage = Encoding.UTF8.GetBytes(json);
var buffer = new ArraySegment<byte>(encodedMessage);
await _webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
Debug.Log($"已发送 JSON 消息:{json}");
}
catch (Exception e)
{
Debug.LogError($"发送消息时出错:{e.Message}");
}
}
private async void OnApplicationQuit()
{
if (_webSocket != null && _webSocket.State == WebSocketState.Open)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "应用程序退出", CancellationToken.None);
_webSocket.Dispose();
Debug.Log("WebSocket 连接已关闭。");
}
}
}
public class BaseResponse
{
public string code { get; set; }
public string message { get; set; }
}
// 返回数据类
public class FishJoinroomresponse
{
public string code { get; set; }
public string message { get; set; }
public FishJoinroomData data { get; set; }
}
public class FishJoinroomData
{
public float balance { get; set; }
public int price { get; set; }
public int surplus_count { get; set; }
public string intro_text { get; set; }
public List<FishJoinroom> items { get; set; }
}
public class FishJoinroom
{
public int id { get; set; }
public string name { get; set; }
public int type { get; set; }
}
public class Fishingresponse
{
public string code { get; set; }
public string message { get; set; }
public FishingData data { get; set; }
}
public class FishingData
{
public float balance { get; set; }
public int price { get; set; }
public int surplus_count { get; set; }
public Prize prize { get; set; }
}
public class Prize
{
public int id { get; set; }
public string name { get; set; }
public int type { get; set; }
}
public class FishBetonresponse
{
public string code { get; set; }
public string message { get; set; }
public FishBetondata data { get; set; }
}
public class FishBetondata
{
public float balance { get; set; }
public int price { get; set; }
public int surplus_count { get; set; }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ec0bc6b6d3f7fd044847aebc4ea31592
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: