Skip to content

Commit 981cb8e

Browse files
author
psygames
committed
code optimization & receive buffer over 8192 bytes error.
1 parent 05e2ded commit 981cb8e

File tree

4 files changed

+99
-162
lines changed

4 files changed

+99
-162
lines changed

Assets/UnityWebSocket/Demo/UnityWebSocketDemo.cs

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ private void OnGUI()
4141
socket.OnMessage += Socket_OnMessage;
4242
socket.OnClose += Socket_OnClose;
4343
socket.OnError += Socket_OnError;
44-
AddLog(string.Format("Connecting...\n"));
44+
AddLog(string.Format("Connecting..."));
4545
socket.ConnectAsync();
4646
}
4747

4848
GUI.enabled = state == WebSocketState.Open;
4949
if (GUILayout.Button(state == WebSocketState.Closing ? "Closing..." : "Close"))
5050
{
51-
AddLog(string.Format("Closing...\n"));
51+
AddLog(string.Format("Closing..."));
5252
socket.CloseAsync();
5353
}
5454
GUILayout.EndHorizontal();
@@ -57,58 +57,41 @@ private void OnGUI()
5757
sendText = GUILayout.TextArea(sendText, GUILayout.MinHeight(50), width);
5858

5959
GUILayout.BeginHorizontal();
60-
if (GUILayout.Button("Send"))
60+
if (GUILayout.Button("Send") && !string.IsNullOrEmpty(sendText))
6161
{
62-
if (!string.IsNullOrEmpty(sendText))
63-
{
64-
socket.SendAsync(sendText);
65-
if (logMessage)
66-
AddLog(string.Format("Send: {0}\n", sendText));
67-
sendCount += 1;
68-
}
62+
socket.SendAsync(sendText);
63+
AddLog(string.Format("Send: {0}", sendText));
64+
sendCount += 1;
6965
}
70-
if (GUILayout.Button("Send Bytes"))
66+
if (GUILayout.Button("Send Bytes") && !string.IsNullOrEmpty(sendText))
7167
{
72-
if (!string.IsNullOrEmpty(sendText))
73-
{
74-
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
75-
socket.SendAsync(bytes);
76-
77-
if (logMessage)
78-
AddLog(string.Format("Send Bytes ({1}): {0}\n", sendText, bytes.Length));
79-
sendCount += 1;
80-
}
68+
var bytes = System.Text.Encoding.UTF8.GetBytes(sendText);
69+
socket.SendAsync(bytes);
70+
AddLog(string.Format("Send Bytes ({1}): {0}", sendText, bytes.Length));
71+
sendCount += 1;
8172
}
82-
if (GUILayout.Button("Send x100"))
73+
if (GUILayout.Button("Send x100") && !string.IsNullOrEmpty(sendText))
8374
{
84-
if (!string.IsNullOrEmpty(sendText))
75+
for (int i = 0; i < 100; i++)
8576
{
86-
for (int i = 0; i < 100; i++)
87-
{
88-
var text = (i + 1).ToString() + ". " + sendText;
89-
socket.SendAsync(text);
90-
91-
if (logMessage)
92-
AddLog(string.Format("Send: {0}\n", text));
93-
sendCount += 1;
94-
}
77+
var text = (i + 1).ToString() + ". " + sendText;
78+
socket.SendAsync(text);
79+
AddLog(string.Format("Send: {0}", text));
80+
sendCount += 1;
9581
}
9682
}
97-
if (GUILayout.Button("Send Bytes x100"))
83+
if (GUILayout.Button("Send Bytes x100") && !string.IsNullOrEmpty(sendText))
9884
{
99-
if (!string.IsNullOrEmpty(sendText))
85+
for (int i = 0; i < 100; i++)
10086
{
101-
for (int i = 0; i < 100; i++)
102-
{
103-
var text = (i + 1).ToString() + ". " + sendText;
104-
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
105-
socket.SendAsync(bytes);
106-
if (logMessage)
107-
AddLog(string.Format("Send Bytes ({1}): {0}\n", text, bytes.Length));
108-
sendCount += 1;
109-
}
87+
var text = (i + 1).ToString() + ". " + sendText;
88+
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
89+
socket.SendAsync(bytes);
90+
AddLog(string.Format("Send Bytes ({1}): {0}", text, bytes.Length));
91+
sendCount += 1;
11092
}
11193
}
94+
11295
GUILayout.EndHorizontal();
11396

11497
GUI.enabled = true;
@@ -132,42 +115,41 @@ private void OnGUI()
132115

133116
private void AddLog(string str)
134117
{
135-
log += str;
136-
// max log
137-
if (log.Length > 32 * 1024)
118+
if (!logMessage) return;
119+
log += str + "\n";
120+
if (log.Length > 4 * 1024)
138121
{
139-
log = log.Substring(16 * 1024);
122+
log = log.Substring(2 * 1024);
140123
}
124+
scrollPos.y = 10000;
141125
}
142126

143127
private void Socket_OnOpen(object sender, OpenEventArgs e)
144128
{
145-
AddLog(string.Format("Connected: {0}\n", address));
129+
AddLog(string.Format("Connected: {0}", address));
146130
}
147131

148132
private void Socket_OnMessage(object sender, MessageEventArgs e)
149133
{
150134
if (e.IsBinary)
151135
{
152-
if (logMessage)
153-
AddLog(string.Format("Receive Bytes ({1}): {0}\n", e.Data, e.RawData.Length));
136+
AddLog(string.Format("Receive Bytes ({1}): {0}", e.Data, e.RawData.Length));
154137
}
155138
else if (e.IsText)
156139
{
157-
if (logMessage)
158-
AddLog(string.Format("Receive: {0}\n", e.Data));
140+
AddLog(string.Format("Receive: {0}", e.Data));
159141
}
160142
receiveCount += 1;
161143
}
162144

163145
private void Socket_OnClose(object sender, CloseEventArgs e)
164146
{
165-
AddLog(string.Format("Closed: StatusCode: {0}, Reason: {1}\n", e.StatusCode, e.Reason));
147+
AddLog(string.Format("Closed: StatusCode: {0}, Reason: {1}", e.StatusCode, e.Reason));
166148
}
167149

168150
private void Socket_OnError(object sender, ErrorEventArgs e)
169151
{
170-
AddLog(string.Format("Error: {0}\n", e.Message));
152+
AddLog(string.Format("Error: {0}", e.Message));
171153
}
172154

173155
private void OnApplicationQuit()

Assets/UnityWebSocket/Scripts/Runtime/Core/MessageEventArgs.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ internal MessageEventArgs(Opcode opcode, string data)
2424
/// Gets the opcode for the message.
2525
/// </summary>
2626
/// <value>
27-
/// <see cref="Opcode.Text"/>, <see cref="Opcode.Binary"/>,
28-
/// or <see cref="Opcode.Ping"/>.
27+
/// <see cref="Opcode.Text"/>, <see cref="Opcode.Binary"/>.
2928
/// </value>
3029
internal Opcode Opcode { get; private set; }
3130

@@ -34,7 +33,7 @@ internal MessageEventArgs(Opcode opcode, string data)
3433
/// </summary>
3534
/// <value>
3635
/// A <see cref="string"/> that represents the message data if its type is
37-
/// text or ping and if decoding it to a string has successfully done;
36+
/// text and if decoding it to a string has successfully done;
3837
/// otherwise, <see langword="null"/>.
3938
/// </value>
4039
public string Data
@@ -75,20 +74,6 @@ public bool IsBinary
7574
}
7675
}
7776

78-
/// <summary>
79-
/// Gets a value indicating whether the message type is ping.
80-
/// </summary>
81-
/// <value>
82-
/// <c>true</c> if the message type is ping; otherwise, <c>false</c>.
83-
/// </value>
84-
public bool IsPing
85-
{
86-
get
87-
{
88-
return Opcode == Opcode.Ping;
89-
}
90-
}
91-
9277
/// <summary>
9378
/// Gets a value indicating whether the message type is text.
9479
/// </summary>
Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
namespace UnityWebSocket
22
{
3-
/// <summary>
4-
/// Indicates the WebSocket frame type.
5-
/// </summary>
6-
/// <remarks>
7-
/// The values of this enumeration are defined in
8-
/// <see href="http://tools.ietf.org/html/rfc6455#section-5.2">
9-
/// Section 5.2</see> of RFC 6455.
10-
/// </remarks>
11-
public enum Opcode : byte
12-
{
133
/// <summary>
14-
/// Equivalent to numeric value 0. Indicates continuation frame.
4+
/// Indicates the WebSocket frame type.
155
/// </summary>
16-
Cont = 0x0,
17-
/// <summary>
18-
/// Equivalent to numeric value 1. Indicates text frame.
19-
/// </summary>
20-
Text = 0x1,
21-
/// <summary>
22-
/// Equivalent to numeric value 2. Indicates binary frame.
23-
/// </summary>
24-
Binary = 0x2,
25-
/// <summary>
26-
/// Equivalent to numeric value 8. Indicates connection close frame.
27-
/// </summary>
28-
Close = 0x8,
29-
/// <summary>
30-
/// Equivalent to numeric value 9. Indicates ping frame.
31-
/// </summary>
32-
Ping = 0x9,
33-
/// <summary>
34-
/// Equivalent to numeric value 10. Indicates pong frame.
35-
/// </summary>
36-
Pong = 0xa
37-
}
6+
/// <remarks>
7+
/// The values of this enumeration are defined in
8+
/// <see href="http://tools.ietf.org/html/rfc6455#section-5.2">
9+
/// Section 5.2</see> of RFC 6455.
10+
/// </remarks>
11+
public enum Opcode : byte
12+
{
13+
/// <summary>
14+
/// Equivalent to numeric value 1. Indicates text frame.
15+
/// </summary>
16+
Text = 0x1,
17+
/// <summary>
18+
/// Equivalent to numeric value 2. Indicates binary frame.
19+
/// </summary>
20+
Binary = 0x2,
21+
/// <summary>
22+
/// Equivalent to numeric value 8. Indicates connection close frame.
23+
/// </summary>
24+
Close = 0x8,
25+
}
3826
}

0 commit comments

Comments
 (0)