diff --git a/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishing.cs.meta b/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishing.cs.meta
index 1b6efd30..b936275d 100644
--- a/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishing.cs.meta
+++ b/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishing.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 29b5ece7ff1f9c946aee00668809f293
+guid: 59ba626460106f54f9a130de3914d56a
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishingPK.cs b/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishingPK.cs
new file mode 100644
index 00000000..69e55bb5
--- /dev/null
+++ b/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishingPK.cs
@@ -0,0 +1,316 @@
+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;
+using UnityEngine.UI;
+public class WebFishingPK : MonoBehaviour
+{
+ private ClientWebSocket _webSocket;
+ private const string WebSocketUri = "ws://47.95.201.243:9527/api/ws";
+ private string AuthorizationValue;
+
+ ///
+ /// 倒计时
+ ///
+ private float lastCallTime = 0f;
+ float remainingTime = 0f;
+ public Text TimeText;
+ private float interval = 1f; // 每秒调用一次
+ private async void Start()
+ {
+ TimeText.gameObject.SetActive(false);
+ Debug.Log(PlayerPrefs.GetString("UserToken"));
+ AuthorizationValue = PlayerPrefs.GetString("UserToken");
+ await ConnectWebSocket();
+
+
+ // 调用发送方法
+ await SendJsonMessage("{ \"code\": \"FISHING_PK\", \"content\": \"{\\\"action\\\":\\\"INFO\\\"}\" }");
+ //await SendJsonMessage(30, 1);
+ }
+
+ private async Task ConnectWebSocket()
+ {
+ _webSocket = new ClientWebSocket();
+
+ // 添加 Authorization 头
+ _webSocket.Options.SetRequestHeader("Authorization", "Bearer " + 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(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(message);
+ Promptmgr.Instance.PromptBubble(baseResponse.message);
+ if (baseResponse != null)
+ {
+ switch (baseResponse.code)
+ {
+ // 解析消息为 钓手PK信息
+ case "FISHING_PK_INFO":
+ // 解析为 FishJoinroomresponse 类型
+ FishPKInforResponse fishResponse = JsonConvert.DeserializeObject(message);
+ //Promptmgr.Instance.PromptBubble(fishResponse.message);
+ if (fishResponse?.data != null)
+ {
+ Debug.Log(fishResponse.data.balance);
+ Debug.Log(fishResponse.data.intro_text);
+ Debug.Log(fishResponse.data.countdown);
+ TimeText.gameObject.SetActive(true);
+ remainingTime = (float)fishResponse.data.countdown;
+ Debug.Log(fishResponse.data.countdown_type);
+ Debug.Log(fishResponse.data.amount_left);
+ Debug.Log(fishResponse.data.amount_right);
+
+ }
+ break;
+ //解析为钓手PK投入
+ case "FISHING_PK_BETTING":
+ // 解析消息为 Fishresponse 对象
+ FishPKBetonResponse fishResponse1 = JsonConvert.DeserializeObject(message);
+ Promptmgr.Instance.PromptBubble(fishResponse1.message);
+ // 检查是否成功反序列化
+ if (fishResponse1 != null && fishResponse1.data != null)
+ {
+ Debug.Log(fishResponse1.data.balance);
+ }
+ else
+ {
+ Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
+ }
+ break;
+ //解析为钓手Pk 倒计时
+ case "FISHING_PK_COUNTDOWN":
+
+ // 解析消息为 Fishresponse 对象
+ FishPKCDResponse fishResponse2 = JsonConvert.DeserializeObject(message);
+ //Promptmgr.Instance.PromptBubble(fishResponse2.message);
+ // 检查是否成功反序列化
+ if (fishResponse2 != null && fishResponse2.data != null)
+ {
+ Debug.Log(fishResponse2.data.countdown);
+ remainingTime = (float)fishResponse2.data.countdown;
+ Debug.Log(fishResponse2.data.countdown_type);
+ Debug.Log(fishResponse2.data.amount_left);
+ Debug.Log(fishResponse2.data.amount_right);
+ }
+ else
+ {
+ Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
+ }
+ break;
+ case "FISHING_PK_SETTLE":
+ FishPKSETResponse fishResponse3 = JsonConvert.DeserializeObject(message);
+ //Promptmgr.Instance.PromptBubble(fishResponse2.message);
+ // 检查是否成功反序列化
+ if (fishResponse3 != null && fishResponse3.data != null)
+ {
+ Debug.Log(fishResponse3.data.balance);
+ Debug.Log(fishResponse3.data.status);
+ Debug.Log(fishResponse3.data.amount);
+ Debug.Log(fishResponse3.data.reward);
+ Debug.Log(fishResponse3.data.weight_left);
+ Debug.Log(fishResponse3.data.weight_right);
+ Promptmgr.Instance.PromptBubble(fishResponse3.data.status);
+ }
+ else
+ {
+ Debug.LogWarning("收到的消息无法解析为 Fishresponse 对象。");
+ }
+ break;
+ }
+ }
+
+
+
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ Debug.LogError($"接收消息时出错:{e.Message}");
+ }
+ }
+ public async Task SendJsonMessage(int count, int postion)
+ {
+ if (_webSocket == null || _webSocket.State != WebSocketState.Open)
+ {
+ Debug.LogError("WebSocket 未连接,无法发送消息。");
+ return;
+ }
+
+ try
+ {
+ var message = new
+ {
+ code = "FISHING_PK",
+ content = $"{{\"action\":\"BETTING\",\"amount\":{count},\"position\":{postion}}}"
+ };
+
+ // 将对象序列化为 JSON 字符串
+ string jsonMessage = JsonConvert.SerializeObject(message);
+ var encodedMessage = Encoding.UTF8.GetBytes(jsonMessage);
+ var buffer = new ArraySegment(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(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 连接已关闭。");
+ }
+ }
+ void UpdateCountdownText(float remainingTime)
+ {
+
+ // 将剩余时间转换为小时、分钟和秒
+ int hours = Mathf.FloorToInt(remainingTime / 3600);
+ int minutes = Mathf.FloorToInt((remainingTime % 3600) / 60);
+ int seconds = Mathf.FloorToInt(remainingTime % 60);
+
+ // 使用格式化字符串显示倒计时(00:00:00)
+ TimeText.text = "倒计时:"+string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
+
+
+ }
+ private void Update()
+ {
+ if (Time.time - lastCallTime >= interval)
+ {
+ // 每秒调用一次的代码
+ if (remainingTime > 0)
+ {
+ remainingTime -= 1;
+ UpdateCountdownText(remainingTime);
+ Debug.Log("进入倒计时");
+ }
+
+ // 更新上次调用时间
+ lastCallTime = Time.time;
+ }
+ }
+}
+
+public class FishPKInforResponse
+{
+ public string code { get; set; }
+ public string message { get; set; }
+ public FishPKInfor data { get; set; }
+}
+public class FishPKInfor
+{
+ public float balance { get; set; }
+
+ public string intro_text { get; set; }
+ public int countdown { get; set; }
+ public int countdown_type { get; set; }
+ public float amount_left { get; set; }
+ public float amount_right { get; set; }
+
+}
+public class FishPKBetonResponse
+{
+ public string code { get; set; }
+ public string message { get; set; }
+ public FishPKdata data { get; set; }
+}
+public class FishPKdata
+{
+ public float balance { get; set; }
+}
+public class FishPKCDResponse
+{
+ public string code { get; set; }
+ public string message { get; set; }
+ public FishPKCDdata data { get; set; }
+}
+public class FishPKCDdata
+{
+ public int countdown { get; set; }
+ public int countdown_type { get; set; }
+ public float amount_left { get; set; }
+ public float amount_right { get; set; }
+}
+public class FishPKSETResponse
+{
+ public string code { get; set; }
+ public string message { get; set; }
+ public FishPKSETdata data { get; set; }
+}
+public class FishPKSETdata
+{
+ public float balance { get; set; }
+
+ public string status { get; set; }
+ public float amount { get; set; }
+ public float reward { get; set; }
+ public float weight_left { get; set; }
+ public float weight_right { get; set; }
+}
diff --git a/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishingPK.cs.meta b/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishingPK.cs.meta
new file mode 100644
index 00000000..1b6efd30
--- /dev/null
+++ b/meng_yao/Assets/script/scene_Main/Thebestfishman/WebFishingPK.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 29b5ece7ff1f9c946aee00668809f293
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: