From 47921ce315e894bbbed712cbf04ee358be345113 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期四, 09 七月 2026 11:08:41 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.1.20:10010/r/Project_SG_client
---
Assets/Plugins/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs | 69 ++++++++++++++++++++++++++++++++++
1 files changed, 69 insertions(+), 0 deletions(-)
diff --git a/Assets/Plugins/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs b/Assets/Plugins/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs
new file mode 100644
index 0000000..32acbd8
--- /dev/null
+++ b/Assets/Plugins/NativeWebSocket/Assets/Samples~/WebSocketExample/Connection.cs
@@ -0,0 +1,69 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+using NativeWebSocket;
+
+public class Connection : MonoBehaviour
+{
+ WebSocket websocket;
+
+ // Start is called before the first frame update
+ async void Start()
+ {
+ // websocket = new WebSocket("ws://echo.websocket.org");
+ websocket = new WebSocket("ws://localhost:3000");
+
+ websocket.OnOpen += () =>
+ {
+ Debug.Log("Connection open!");
+ };
+
+ websocket.OnError += (e) =>
+ {
+ Debug.Log("Error! " + e);
+ };
+
+ websocket.OnClose += (e) =>
+ {
+ Debug.Log("Connection closed!");
+ };
+
+ websocket.OnMessage += (bytes) =>
+ {
+ // Reading a plain text message
+ var message = System.Text.Encoding.UTF8.GetString(bytes);
+ Debug.Log("Received OnMessage! (" + bytes.Length + " bytes) " + message);
+ };
+
+ // Keep sending messages at every 0.3s
+ InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);
+
+ await websocket.Connect();
+ }
+
+ void Update()
+ {
+ #if !UNITY_WEBGL || UNITY_EDITOR
+ websocket.DispatchMessageQueue();
+ #endif
+ }
+
+ async void SendWebSocketMessage()
+ {
+ if (websocket.State == WebSocketState.Open)
+ {
+ // Sending bytes
+ await websocket.Send(new byte[] { 10, 20, 30 });
+
+ // Sending plain text
+ await websocket.SendText("plain text message");
+ }
+ }
+
+ private async void OnApplicationQuit()
+ {
+ await websocket.Close();
+ }
+}
--
Gitblit v1.8.0