Skip to content

Commit 86598bd

Browse files
author
yinlong
committed
fix jslib 添加 send str ,get socket state,重构事件处理
1 parent 51f41d2 commit 86598bd

File tree

11 files changed

+280
-260
lines changed

11 files changed

+280
-260
lines changed

TestWebSocketServer/TestWebSocketServer/Form1.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected override void OnOpen()
8686

8787
protected override void OnMessage(MessageEventArgs e)
8888
{
89-
string msg = Encoding.UTF8.GetString(e.RawData);
89+
string msg = e.Data;
9090
Form1.instance.Log(addr, "Receive From :" + ID + "\n" + msg);
9191
SendMessage("Got [" + msg + "] at " + DateTime.Now);
9292
}
@@ -99,8 +99,7 @@ protected override void OnClose(CloseEventArgs e)
9999

100100
public void SendMessage(string msg)
101101
{
102-
byte[] data = Encoding.UTF8.GetBytes(msg);
103-
Sessions.SendToAsync(data, ID, (a) => { });
102+
Sessions.SendToAsync(msg, ID, (a) => { });
104103
}
105104
}
106105
}
Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11

22
var WebSocketJS =
33
{
4-
$RECEIVER_NAME:{},
5-
$OPEN_METHOD_NAME:{},
6-
$CLOSE_METHOD_NAME:{},
7-
$RECEIVE_METHOD_NAME:{},
8-
$RECEIVE_STRING_METHOD_NAME:{},
9-
$ERROR_METHOD_NAME:{},
4+
$RECEIVER_NAME: {},
5+
$OPEN_METHOD_NAME: {},
6+
$CLOSE_METHOD_NAME: {},
7+
$MESSAGE_METHOD_NAME: {},
8+
$ERROR_METHOD_NAME: {},
109
$webSocketMap: {},
1110

1211
$Initialize: function()
@@ -15,8 +14,7 @@
1514
RECEIVER_NAME = "WebSocketReceiver";
1615
OPEN_METHOD_NAME = "OnOpen";
1716
CLOSE_METHOD_NAME = "OnClose";
18-
RECEIVE_METHOD_NAME = "OnReceive";
19-
RECEIVE_STRING_METHOD_NAME = "OnReceiveString";
17+
MESSAGE_METHOD_NAME = "OnMessage";
2018
ERROR_METHOD_NAME = "OnError";
2119
},
2220

@@ -37,11 +35,11 @@
3735
webSocket.onmessage = function(e)
3836
{
3937
if (e.data instanceof Blob)
40-
OnMessage(address, e.data);
41-
else if(typeOf event.data === String) {
42-
OnMessageString(address, e.data);
38+
OnMessage(address, 2, e.data);
39+
else if(typeof e.data == 'string')
40+
OnMessage(address, 1, e.data);
4341
else
44-
OnError(address, "msg is not a blob instance");
42+
OnError(address, "onmessage can not recognize msg type !");
4543
};
4644

4745
webSocket.onopen = function(e)
@@ -51,19 +49,13 @@
5149

5250
webSocket.onclose = function(e)
5351
{
54-
OnClose(address);
55-
if(e.code != 1000)
56-
{
57-
if(e.reason != null && e.reason.length > 0)
58-
OnError(address, e.reason);
59-
else
60-
OnError(address, GetCloseReason(e.code));
61-
}
52+
OnClose(address, e.code, e.reason, e.wasClean);
6253
};
6354

6455
webSocket.onerror = function(e)
6556
{
6657
// can not catch the error reason, only use for debug.
58+
OnError(address, e.message);
6759
};
6860

6961
webSocketMap.set(address, webSocket);
@@ -80,50 +72,66 @@
8072
},
8173

8274
// call by unity
83-
CloseJS: function (addressPtr)
75+
SendStrJS: function (addressPtr, msgPtr)
8476
{
8577
var address = Pointer_stringify(addressPtr);
78+
var msg = Pointer_stringify(msgPtr);
8679
if(webSocketMap.has(address))
87-
webSocketMap.get(address).close();
80+
webSocketMap.get(address).send(msg);
8881
else
89-
OnError(address, "close with a WebSocket not Instantiated");
82+
OnError(address, "send msg with a WebSocket not Instantiated");
9083
},
9184

9285
// call by unity
93-
GetReadyStateJS: function (addressPtr)
86+
CloseJS: function (addressPtr)
9487
{
9588
var address = Pointer_stringify(addressPtr);
9689
if(webSocketMap.has(address))
97-
webSocketMap.get(address).readyState;
90+
webSocketMap.get(address).close();
9891
else
99-
OnError(address, "get readyState with a WebSocket not Instantiated");
92+
OnError(address, "close with a WebSocket not Instantiated");
10093
},
10194

102-
$OnMessageString: function(address, str)
95+
// call by unity
96+
GetReadyStateJS: function (addressPtr)
10397
{
104-
SendMessage(RECEIVER_NAME, RECEIVE_STRING_METHOD_NAME, str);
98+
var address = Pointer_stringify(addressPtr);
99+
if(!(webSocketMap instanceof Map))
100+
return 0;
101+
if(webSocketMap.has(address))
102+
return webSocketMap.get(address).readyState;
103+
return 0;
105104
},
106105

107-
$OnMessage: function(address, blobData)
106+
$OnMessage: function(address, opcode, data)
108107
{
109-
var reader = new FileReader();
110-
reader.addEventListener("loadend", function()
108+
var addr_opcode_data = address + "_" + opcode + "_";
109+
// blobData
110+
if(opcode == 2)
111111
{
112-
// format : address_data, (address and data split with "_")
113-
// the data format is hex string
114-
var msg = address + "_";
115-
var array = new Uint8Array(reader.result);
116-
for(var i = 0; i < array.length; i++)
112+
var reader = new FileReader();
113+
reader.addEventListener("loadend", function()
117114
{
118-
var b = array[i];
119-
if(b < 16)
120-
msg += "0" + b.toString(16);
121-
else
122-
msg += b.toString(16);
123-
}
124-
SendMessage(RECEIVER_NAME, RECEIVE_METHOD_NAME, msg);
125-
});
126-
reader.readAsArrayBuffer(blobData);
115+
// format : address_data, (address and data split with "_")
116+
// the data format is hex string
117+
var array = new Uint8Array(reader.result);
118+
for(var i = 0; i < array.length; i++)
119+
{
120+
var b = array[i];
121+
if(b < 16)
122+
addr_opcode_data += "0" + b.toString(16);
123+
else
124+
addr_opcode_data += b.toString(16);
125+
}
126+
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, addr_opcode_data);
127+
});
128+
reader.readAsArrayBuffer(data);
129+
}
130+
else
131+
{
132+
addr_opcode_data += data;
133+
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, addr_opcode_data);
134+
}
127135
},
128136

129137
$OnOpen: function(address)
@@ -135,59 +143,26 @@
135143
{
136144
if(webSocketMap.get(address))
137145
webSocketMap.delete(address);
138-
SendMessage(RECEIVER_NAME, CLOSE_METHOD_NAME, address, code, reason, wasClean);
146+
SendMessage(RECEIVER_NAME, CLOSE_METHOD_NAME, address+"_"+code+"_"+reason+"_"+wasClean);
139147
},
140148

141149
$OnError: function(address, errorMsg)
142150
{
143151
var combinedMsg = address + "_" + errorMsg;
144-
SendMessage(RECEIVER_NAME, ERROR_METHOD_NAME ,combinedMsg);
152+
SendMessage(RECEIVER_NAME, ERROR_METHOD_NAME, combinedMsg);
145153
},
146-
147-
$GetCloseReason: function(code)
148-
{
149-
var error = "";
150-
switch (code)
151-
{
152-
case 1001:
153-
error = "Endpoint going away.";
154-
break;
155-
case 1002:
156-
error = "Protocol error.";
157-
break;
158-
case 1003:
159-
error = "Unsupported message.";
160-
break;
161-
case 1005:
162-
error = "No status.";
163-
break;
164-
case 1006:
165-
error = "Abnormal disconnection.";
166-
break;
167-
case 1009:
168-
error = "Data frame too large.";
169-
break;
170-
default:
171-
error = "Error Code " + code;
172-
break;
173-
}
174-
return error;
175-
},
176-
177154
};
178155

179156
// Auto add to depends
180157
autoAddDeps(WebSocketJS, '$RECEIVER_NAME');
181158
autoAddDeps(WebSocketJS, '$OPEN_METHOD_NAME');
182159
autoAddDeps(WebSocketJS, '$CLOSE_METHOD_NAME');
183-
autoAddDeps(WebSocketJS, '$RECEIVE_STRING_METHOD_NAME');
184-
autoAddDeps(WebSocketJS, '$RECEIVE_METHOD_NAME');
160+
autoAddDeps(WebSocketJS, '$MESSAGE_METHOD_NAME');
161+
autoAddDeps(WebSocketJS, '$ERROR_METHOD_NAME');
185162
autoAddDeps(WebSocketJS, '$webSocketMap');
186163
autoAddDeps(WebSocketJS, '$Initialize');
187164
autoAddDeps(WebSocketJS, '$OnMessage');
188-
autoAddDeps(WebSocketJS, '$OnMessageString');
189165
autoAddDeps(WebSocketJS, '$OnOpen');
190166
autoAddDeps(WebSocketJS, '$OnClose');
191167
autoAddDeps(WebSocketJS, '$OnError');
192-
autoAddDeps(WebSocketJS, '$GetCloseReason');
193168
mergeInto(LibraryManager.library, WebSocketJS);

UnityWebSocket/Assets/Scripts/Plugins/UnityWebSocket/CloseEventArgs.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,25 @@ internal CloseEventArgs()
5959
}
6060

6161
internal CloseEventArgs(ushort code)
62-
: this(code, null)
62+
: this(code, null, false)
6363
{
6464
}
6565

6666
internal CloseEventArgs(CloseStatusCode code)
67-
: this((ushort)code, null)
67+
: this((ushort)code, null, false)
6868
{
6969
}
7070

71-
internal CloseEventArgs(CloseStatusCode code, string reason)
72-
: this((ushort)code, reason)
71+
internal CloseEventArgs(CloseStatusCode code, string reason, bool wasClean)
72+
: this((ushort)code, reason, wasClean)
7373
{
7474
}
7575

76-
internal CloseEventArgs(ushort code, string reason)
76+
internal CloseEventArgs(ushort code, string reason, bool wasClean)
7777
{
7878
_code = code;
7979
_reason = reason;
80+
_clean = wasClean;
8081
}
8182

8283
#endregion

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1}
41+
m_IndirectSpecularColor: {r: 0.37311947, g: 0.38074005, b: 0.35872722, a: 1}
4242
--- !u!157 &3
4343
LightmapSettings:
4444
m_ObjectHideFlags: 0
@@ -362,7 +362,7 @@ RectTransform:
362362
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
363363
m_AnchorMin: {x: 0, y: 1}
364364
m_AnchorMax: {x: 1, y: 1}
365-
m_AnchoredPosition: {x: -0.0000034321888, y: 0.000030117406}
365+
m_AnchoredPosition: {x: -0.0000019468666, y: -0.00002671818}
366366
m_SizeDelta: {x: 0, y: 0}
367367
m_Pivot: {x: 0, y: 1}
368368
--- !u!114 &208146964
@@ -3717,8 +3717,8 @@ MonoBehaviour:
37173717
m_TargetGraphic: {fileID: 837294422}
37183718
m_HandleRect: {fileID: 837294421}
37193719
m_Direction: 2
3720-
m_Value: 0
3721-
m_Size: 1
3720+
m_Value: 1
3721+
m_Size: 0.99999994
37223722
m_NumberOfSteps: 0
37233723
m_OnValueChanged:
37243724
m_PersistentCalls:

0 commit comments

Comments
 (0)