Skip to content

Commit b1729ae

Browse files
author
yinlong
committed
test
1 parent f0c4b3b commit b1729ae

File tree

16 files changed

+1559
-372
lines changed

16 files changed

+1559
-372
lines changed

UnityWebSocket/Assembly-CSharp-Editor.csproj

Lines changed: 440 additions & 111 deletions
Large diffs are not rendered by default.

UnityWebSocket/Assembly-CSharp.csproj

Lines changed: 425 additions & 103 deletions
Large diffs are not rendered by default.

UnityWebSocket/Assets/Plugins/WebSocketJS/WebSocketJS.jslib

Lines changed: 150 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,155 @@
11
var WebSocketJS =
22
{
3-
$RECEIVER_NAME: {},
4-
$OPEN_METHOD_NAME: {},
5-
$CLOSE_METHOD_NAME: {},
6-
$MESSAGE_METHOD_NAME: {},
7-
$ERROR_METHOD_NAME: {},
8-
$webSocketMap: {},
9-
10-
$Initialize: function()
11-
{
12-
webSocketMap = new Map();
13-
RECEIVER_NAME = "[WebSocketReceiver]";
14-
OPEN_METHOD_NAME = "OnOpen";
15-
CLOSE_METHOD_NAME = "OnClose";
16-
MESSAGE_METHOD_NAME = "OnMessage";
17-
ERROR_METHOD_NAME = "OnError";
18-
},
19-
20-
// call by unity
21-
ConnectJS: function(addressaPtr)
22-
{
23-
if(!(webSocketMap instanceof Map))
24-
Initialize();
25-
26-
var address = Pointer_stringify(addressaPtr);
27-
if(webSocketMap.has(address))
28-
{
29-
OnError(address, "Duplicated address: " + address);
30-
return;
31-
}
32-
33-
var webSocket = new WebSocket(address);
34-
webSocket.onmessage = function(e)
35-
{
36-
if (e.data instanceof Blob)
37-
OnMessage(address, 2, e.data);
38-
else if(typeof e.data == 'string')
39-
OnMessage(address, 1, e.data);
40-
else
41-
OnError(address, "onmessage can not recognize msg type !");
42-
};
43-
44-
webSocket.onopen = function(e)
45-
{
46-
OnOpen(address);
47-
};
48-
49-
webSocket.onclose = function(e)
50-
{
51-
OnClose(address, e.code, e.reason, e.wasClean);
52-
};
53-
54-
webSocket.onerror = function(e)
55-
{
56-
// can not catch the error reason, only use for debug.
57-
// see this page https://stackoverflow.com/questions/18803971/websocket-onerror-how-to-read-error-description
58-
OnError(address, "a websocket error occured.");
59-
};
60-
61-
webSocketMap.set(address, webSocket);
62-
},
63-
64-
// call by unity
65-
SendJS: function (addressPtr, msgPtr, length)
66-
{
67-
var address = Pointer_stringify(addressPtr);
68-
if(webSocketMap.has(address))
69-
webSocketMap.get(address).send(HEAPU8.buffer.slice(msgPtr, msgPtr + length));
70-
else
71-
OnError(address, "send msg binary with a WebSocket not Instantiated");
72-
},
73-
74-
// call by unity
75-
SendStrJS: function (addressPtr, msgPtr)
76-
{
77-
var address = Pointer_stringify(addressPtr);
78-
var msg = Pointer_stringify(msgPtr);
79-
if(webSocketMap.has(address))
80-
webSocketMap.get(address).send(msg);
81-
else
82-
OnError(address, "send msg string with a WebSocket not Instantiated");
83-
},
84-
85-
// call by unity
86-
CloseJS: function (addressPtr)
87-
{
88-
var address = Pointer_stringify(addressPtr);
89-
if(webSocketMap.has(address))
90-
webSocketMap.get(address).close();
91-
else
92-
OnError(address, "close with a WebSocket not Instantiated");
93-
},
94-
95-
// call by unity
96-
GetReadyStateJS: function (addressPtr)
97-
{
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;
104-
},
105-
106-
$OnMessage: function(address, opcode, data)
107-
{
108-
var combinedMsg = address + "_" + opcode + "_";
109-
if(opcode == 2) // blob data
110-
{
111-
var reader = new FileReader();
112-
reader.addEventListener("loadend", function()
113-
{
114-
// data format to hex string
115-
var array = new Uint8Array(reader.result);
116-
for(var i = 0; i < array.length; i++)
117-
{
118-
var b = array[i];
119-
if(b < 16)
120-
combinedMsg += "0" + b.toString(16);
121-
else
122-
combinedMsg += b.toString(16);
123-
}
124-
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, combinedMsg);
125-
});
126-
reader.readAsArrayBuffer(data);
127-
}
128-
else // string data
129-
{
130-
combinedMsg += data;
131-
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, combinedMsg);
132-
}
133-
},
134-
135-
$OnOpen: function(address)
136-
{
137-
SendMessage(RECEIVER_NAME, OPEN_METHOD_NAME, address);
138-
},
139-
140-
$OnClose: function(address, code, reason, wasClean)
141-
{
142-
if(webSocketMap.get(address))
143-
webSocketMap.delete(address);
144-
var combinedMsg = address + "_" + code + "_" + reason + "_" + wasClean;
145-
SendMessage(RECEIVER_NAME, CLOSE_METHOD_NAME, combinedMsg);
146-
},
147-
148-
$OnError: function(address, errorMsg)
149-
{
150-
var combinedMsg = address + "_" + errorMsg;
151-
SendMessage(RECEIVER_NAME, ERROR_METHOD_NAME, combinedMsg);
152-
},
3+
$RECEIVER_NAME: {},
4+
$OPEN_METHOD_NAME: {},
5+
$CLOSE_METHOD_NAME: {},
6+
$MESSAGE_METHOD_NAME: {},
7+
$ERROR_METHOD_NAME: {},
8+
$webSocketMap: {},
9+
10+
$Initialize: function()
11+
{
12+
webSocketMap = new Map();
13+
RECEIVER_NAME = "[WebSocketReceiver]";
14+
OPEN_METHOD_NAME = "OnOpen";
15+
CLOSE_METHOD_NAME = "OnClose";
16+
MESSAGE_METHOD_NAME = "OnMessage";
17+
ERROR_METHOD_NAME = "OnError";
18+
},
19+
20+
// call by unity
21+
ConnectJS: function(addressaPtr)
22+
{
23+
if(!(webSocketMap instanceof Map))
24+
Initialize();
25+
26+
var address = Pointer_stringify(addressaPtr);
27+
if(webSocketMap.has(address))
28+
{
29+
OnError(address, "Duplicated address: " + address);
30+
return;
31+
}
32+
33+
var webSocket = new WebSocket(address);
34+
webSocket.onmessage = function(e)
35+
{
36+
if (e.data instanceof Blob)
37+
OnMessage(address, 2, e.data);
38+
else if(typeof e.data == 'string')
39+
OnMessage(address, 1, e.data);
40+
else
41+
OnError(address, "onmessage can not recognize msg type !");
42+
};
43+
44+
webSocket.onopen = function(e)
45+
{
46+
OnOpen(address);
47+
};
48+
49+
webSocket.onclose = function(e)
50+
{
51+
OnClose(address, e.code, e.reason, e.wasClean);
52+
};
53+
54+
webSocket.onerror = function(e)
55+
{
56+
// can not catch the error reason, only use for debug.
57+
// see this page https://stackoverflow.com/questions/18803971/websocket-onerror-how-to-read-error-description
58+
OnError(address, "a websocket error occured.");
59+
};
60+
61+
webSocketMap.set(address, webSocket);
62+
},
63+
64+
// call by unity
65+
SendJS: function (addressPtr, msgPtr, length)
66+
{
67+
var address = Pointer_stringify(addressPtr);
68+
if(webSocketMap.has(address))
69+
webSocketMap.get(address).send(HEAPU8.buffer.slice(msgPtr, msgPtr + length));
70+
else
71+
OnError(address, "send msg binary with a WebSocket not Instantiated");
72+
},
73+
74+
// call by unity
75+
SendStrJS: function (addressPtr, msgPtr)
76+
{
77+
var address = Pointer_stringify(addressPtr);
78+
var msg = Pointer_stringify(msgPtr);
79+
if(webSocketMap.has(address))
80+
webSocketMap.get(address).send(msg);
81+
else
82+
OnError(address, "send msg string with a WebSocket not Instantiated");
83+
},
84+
85+
// call by unity
86+
CloseJS: function (addressPtr)
87+
{
88+
var address = Pointer_stringify(addressPtr);
89+
if(webSocketMap.has(address))
90+
webSocketMap.get(address).close();
91+
else
92+
OnError(address, "close with a WebSocket not Instantiated");
93+
},
94+
95+
// call by unity
96+
GetReadyStateJS: function (addressPtr)
97+
{
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;
104+
},
105+
106+
$OnMessage: function(address, opcode, data)
107+
{
108+
var combinedMsg = address + "_" + opcode + "_";
109+
if(opcode == 2) // blob data
110+
{
111+
var reader = new FileReader();
112+
reader.addEventListener("loadend", function()
113+
{
114+
// data format to hex string
115+
var array = new Uint8Array(reader.result);
116+
for(var i = 0; i < array.length; i++)
117+
{
118+
var b = array[i];
119+
if(b < 16)
120+
combinedMsg += "0" + b.toString(16);
121+
else
122+
combinedMsg += b.toString(16);
123+
}
124+
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, combinedMsg);
125+
});
126+
reader.readAsArrayBuffer(data);
127+
}
128+
else // string data
129+
{
130+
combinedMsg += data;
131+
SendMessage(RECEIVER_NAME, MESSAGE_METHOD_NAME, combinedMsg);
132+
}
133+
},
134+
135+
$OnOpen: function(address)
136+
{
137+
SendMessage(RECEIVER_NAME, OPEN_METHOD_NAME, address);
138+
},
139+
140+
$OnClose: function(address, code, reason, wasClean)
141+
{
142+
if(webSocketMap.get(address))
143+
webSocketMap.delete(address);
144+
var combinedMsg = address + "_" + code + "_" + reason + "_" + wasClean;
145+
SendMessage(RECEIVER_NAME, CLOSE_METHOD_NAME, combinedMsg);
146+
},
147+
148+
$OnError: function(address, errorMsg)
149+
{
150+
var combinedMsg = address + "_" + errorMsg;
151+
SendMessage(RECEIVER_NAME, ERROR_METHOD_NAME, combinedMsg);
152+
},
153153
};
154154

155155
// Auto add to depends

0 commit comments

Comments
 (0)