Skip to content

Commit a5bfee7

Browse files
committed
handle some bugs
1 parent c3b3e13 commit a5bfee7

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Example/Examlple.unity

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,43 +156,43 @@ MonoBehaviour:
156156
m_Name:
157157
m_EditorClassIdentifier:
158158
url: ws://echo.websocket.org
159-
--- !u!1 &729950553
159+
--- !u!1 &902376145
160160
GameObject:
161161
m_ObjectHideFlags: 0
162162
m_CorrespondingSourceObject: {fileID: 0}
163163
m_PrefabInstance: {fileID: 0}
164164
m_PrefabAsset: {fileID: 0}
165165
serializedVersion: 6
166166
m_Component:
167-
- component: {fileID: 729950556}
168-
- component: {fileID: 729950555}
169-
- component: {fileID: 729950554}
167+
- component: {fileID: 902376148}
168+
- component: {fileID: 902376147}
169+
- component: {fileID: 902376146}
170170
m_Layer: 0
171171
m_Name: Camera
172-
m_TagString: MainCamera
172+
m_TagString: Untagged
173173
m_Icon: {fileID: 0}
174174
m_NavMeshLayer: 0
175175
m_StaticEditorFlags: 0
176176
m_IsActive: 1
177-
--- !u!81 &729950554
177+
--- !u!81 &902376146
178178
AudioListener:
179179
m_ObjectHideFlags: 0
180180
m_CorrespondingSourceObject: {fileID: 0}
181181
m_PrefabInstance: {fileID: 0}
182182
m_PrefabAsset: {fileID: 0}
183-
m_GameObject: {fileID: 729950553}
183+
m_GameObject: {fileID: 902376145}
184184
m_Enabled: 1
185-
--- !u!20 &729950555
185+
--- !u!20 &902376147
186186
Camera:
187187
m_ObjectHideFlags: 0
188188
m_CorrespondingSourceObject: {fileID: 0}
189189
m_PrefabInstance: {fileID: 0}
190190
m_PrefabAsset: {fileID: 0}
191-
m_GameObject: {fileID: 729950553}
191+
m_GameObject: {fileID: 902376145}
192192
m_Enabled: 1
193193
serializedVersion: 2
194194
m_ClearFlags: 2
195-
m_BackGroundColor: {r: 0.3018868, g: 0.3018868, b: 0.3018868, a: 0}
195+
m_BackGroundColor: {r: 0.3301887, g: 0.3301887, b: 0.3301887, a: 0}
196196
m_projectionMatrixMode: 1
197197
m_SensorSize: {x: 36, y: 24}
198198
m_LensShift: {x: 0, y: 0}
@@ -209,7 +209,7 @@ Camera:
209209
field of view: 60
210210
orthographic: 0
211211
orthographic size: 5
212-
m_Depth: -1
212+
m_Depth: 0
213213
m_CullingMask:
214214
serializedVersion: 2
215215
m_Bits: 4294967295
@@ -224,15 +224,15 @@ Camera:
224224
m_OcclusionCulling: 1
225225
m_StereoConvergence: 10
226226
m_StereoSeparation: 0.022
227-
--- !u!4 &729950556
227+
--- !u!4 &902376148
228228
Transform:
229229
m_ObjectHideFlags: 0
230230
m_CorrespondingSourceObject: {fileID: 0}
231231
m_PrefabInstance: {fileID: 0}
232232
m_PrefabAsset: {fileID: 0}
233-
m_GameObject: {fileID: 729950553}
233+
m_GameObject: {fileID: 902376145}
234234
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
235-
m_LocalPosition: {x: 0, y: 1, z: -10}
235+
m_LocalPosition: {x: 0, y: 0, z: 0}
236236
m_LocalScale: {x: 1, y: 1, z: 1}
237237
m_Children: []
238238
m_Father: {fileID: 0}

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Example/TestWebSocket.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ private void Socket_OnError(object sender, ErrorEventArgs e)
3939
string sendText = "";
4040
string message = "";
4141
int messageCount;
42+
Vector2 scrollPos;
4243

4344
private void OnGUI()
4445
{
4546
var scale = Screen.width / 800f;
46-
GUI.matrix = Matrix4x4.TRS(new Vector3(200 * scale, 10, 0), Quaternion.identity, new Vector3(scale, scale, 1));
47+
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scale, scale, 1));
48+
var width = GUILayout.Width(Screen.width / scale - 10);
4749

48-
GUILayout.Label(string.Format("State: {0}", socket.ReadyState));
49-
50-
GUILayout.Label("URL: ");
51-
url = GUILayout.TextField(url);
50+
GUILayout.Label(string.Format("State: {0}", socket.ReadyState), width);
51+
GUILayout.Label("URL: ", width);
52+
url = GUILayout.TextField(url, width);
5253

5354
GUILayout.BeginHorizontal();
5455
GUI.enabled = socket.ReadyState == WebSocketState.Closed;
@@ -65,7 +66,7 @@ private void OnGUI()
6566
GUILayout.EndHorizontal();
6667

6768
GUILayout.Label("Text: ");
68-
sendText = GUILayout.TextArea(sendText);
69+
sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width);
6970

7071
if (GUILayout.Button("Send"))
7172
{
@@ -79,7 +80,10 @@ private void OnGUI()
7980
message = "";
8081
messageCount = 0;
8182
}
83+
84+
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MaxHeight(Screen.height / scale - 250), width);
8285
GUILayout.Label(message);
86+
GUILayout.EndScrollView();
8387

8488
}
8589
}

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Implementation/Common/WebSocket.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
namespace UnityWebSocket.Common
1111
{
12+
/// <summary>
13+
/// <para>WebSocket indicate a network connection.</para>
14+
/// <para>It can be connecting, connected, closing or closed state. </para>
15+
/// <para>You can send and receive messages by using it.</para>
16+
/// <para>Register receive callback for handling received messages.</para>
17+
/// <para>WebSocket 表示一个网络连接,</para>
18+
/// <para>它可以是 connecting connected closing closed 状态,</para>
19+
/// <para>可以发送和接收消息,</para>
20+
/// <para>接收消息处理的地方注册消息回调即可。</para>
21+
/// </summary>
1222
public class WebSocket : IWebSocket
1323
{
1424
public string Address { get; private set; }
@@ -52,6 +62,11 @@ private void TaskNew(Func<Task> function)
5262

5363
public void ConnectAsync(string address)
5464
{
65+
if (cts != null || socket != null)
66+
{
67+
HandleOnError("socket is busy.");
68+
return;
69+
}
5570
this.Address = address;
5671
var uri = new Uri(Address);
5772
cts = new CancellationTokenSource();
@@ -173,23 +188,40 @@ private async Task SendThread()
173188
private async Task ReceiveThread()
174189
{
175190
isReceiveThreadRunning = true;
176-
var buffer = new ArraySegment<byte>(new byte[1024 * 1024]);
191+
192+
var buffer = new byte[1024 * 1024];
193+
var bufferCount = 0;
194+
var segment = new ArraySegment<byte>(buffer, 0, buffer.Length);
177195
while (!cts.IsCancellationRequested)
178196
{
179-
WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, cts.Token);
180-
byte[] data = new byte[result.Count];
181-
for (int i = 0; i < result.Count; i++)
197+
WebSocketReceiveResult result = await socket.ReceiveAsync(segment, cts.Token);
198+
bufferCount += result.Count;
199+
segment = new ArraySegment<byte>(buffer, bufferCount, buffer.Length - bufferCount);
200+
201+
if (!result.EndOfMessage)
202+
continue;
203+
204+
byte[] data = new byte[bufferCount];
205+
for (int i = 0; i < bufferCount; i++)
182206
{
183-
data[i] = buffer.Array[buffer.Offset + i];
207+
data[i] = buffer[i];
184208
}
209+
210+
bufferCount = 0;
211+
segment = new ArraySegment<byte>(buffer, 0, buffer.Length);
212+
185213
if (result.MessageType == WebSocketMessageType.Binary)
214+
{
186215
HandleOnMessage(Opcode.Binary, data);
216+
}
187217
else if (result.MessageType == WebSocketMessageType.Text)
218+
{
188219
HandleOnMessage(Opcode.Text, data);
220+
}
189221
else
190222
{
191223
cts.Cancel();
192-
HandleOnClose((ushort)result.CloseStatus, result.CloseStatusDescription, result.EndOfMessage);
224+
HandleOnClose((ushort)result.CloseStatus, result.CloseStatusDescription, true);
193225
}
194226
}
195227

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/Implementation/Uniform/WebSocket.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
namespace UnityWebSocket
44
{
55
/// <summary>
6-
/// For All Platform
6+
/// <para>WebSocket indicate a network connection.</para>
7+
/// <para>It can be connecting, connected, closing or closed state. </para>
8+
/// <para>You can send and receive messages by using it.</para>
9+
/// <para>Register receive callback for handling received messages.</para>
10+
/// <para>WebSocket 表示一个网络连接,</para>
11+
/// <para>它可以是 connecting connected closing closed 状态,</para>
12+
/// <para>可以发送和接收消息,</para>
13+
/// <para>接收消息处理的地方注册消息回调即可。</para>
714
/// </summary>
815
public class WebSocket : IWebSocket
916
{

0 commit comments

Comments
 (0)