-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathSocketIOTest.js
More file actions
278 lines (208 loc) · 11.4 KB
/
Copy pathSocketIOTest.js
File metadata and controls
278 lines (208 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/****************************************************************************
Copyright (c) 2014 cocos2d-x.org http://www.cocos2d-x.org
Created by Chris Hannon 2014 http://www.channon.us
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var SocketIO = SocketIO || io;
var SocketIOTestLayer = cc.Layer.extend({
_sioClient: null,
_sioEndpoint: null,
_sioClientStatus: null,
init: function () {
var winSize = cc.Director.getInstance().getWinSize();
var MARGIN = 40;
var SPACE = 35;
var label = cc.LabelTTF.create("SocketIO Test", "Arial", 28);
label.setPosition(cc.p(winSize.width / 2, winSize.height - MARGIN));
this.addChild(label, 0);
var menuRequest = cc.Menu.create();
menuRequest.setPosition(cc.p(0, 0));
this.addChild(menuRequest);
// Test to create basic client in the default namespace
var labelSIOClient = cc.LabelTTF.create("Open SocketIO Client", "Arial", 22);
labelSIOClient.setAnchorPoint(cc.p(0,0));
var itemSIOClient = cc.MenuItemLabel.create(labelSIOClient, this.onMenuSIOClientClicked, this);
itemSIOClient.setPosition(cc.p(labelSIOClient.getContentSize().width / 2 + MARGIN, winSize.height - MARGIN - SPACE));
menuRequest.addChild(itemSIOClient);
// Test to create a client at the endpoint '/testpoint'
var labelSIOEndpoint = cc.LabelTTF.create("Open SocketIO Endpoint", "Arial", 22);
labelSIOEndpoint.setAnchorPoint(cc.p(0,0));
var itemSIOEndpoint = cc.MenuItemLabel.create(labelSIOEndpoint, this.onMenuSIOEndpointClicked, this);
itemSIOEndpoint.setPosition(cc.p(winSize.width - (labelSIOEndpoint.getContentSize().width / 2 + MARGIN), winSize.height - MARGIN - SPACE));
menuRequest.addChild(itemSIOEndpoint);
// Test sending message to default namespace
var labelTestMessage = cc.LabelTTF.create("Send Test Message", "Arial", 22);
labelTestMessage.setAnchorPoint(cc.p(0,0));
var itemTestMessage = cc.MenuItemLabel.create(labelTestMessage, this.onMenuTestMessageClicked, this);
itemTestMessage.setPosition(cc.p(labelTestMessage.getContentSize().width / 2 + MARGIN, winSize.height - MARGIN - 2 * SPACE));
menuRequest.addChild(itemTestMessage);
// Test sending message to the endpoint '/testpoint'
var labelTestMessageEndpoint = cc.LabelTTF.create("Test Endpoint Message", "Arial", 22);
labelTestMessageEndpoint.setAnchorPoint(cc.p(0,0));
var itemTestMessageEndpoint = cc.MenuItemLabel.create(labelTestMessageEndpoint, this.onMenuTestMessageEndpointClicked, this);
itemTestMessageEndpoint.setPosition(cc.p(winSize.width - (labelTestMessageEndpoint.getContentSize().width / 2 + MARGIN), winSize.height - MARGIN - 2 * SPACE));
menuRequest.addChild(itemTestMessageEndpoint);
// Test sending event 'echotest' to default namespace
var labelTestEvent = cc.LabelTTF.create("Send Test Event", "Arial", 22);
labelTestEvent.setAnchorPoint(cc.p(0,0));
var itemTestEvent = cc.MenuItemLabel.create(labelTestEvent, this.onMenuTestEventClicked, this);
itemTestEvent.setPosition(cc.p(labelTestEvent.getContentSize().width / 2 + MARGIN, winSize.height - MARGIN - 3 * SPACE));
menuRequest.addChild(itemTestEvent);
// Test sending event 'echotest' to the endpoint '/testpoint'
var labelTestEventEndpoint = cc.LabelTTF.create("Test Endpoint Event", "Arial", 22);
labelTestEventEndpoint.setAnchorPoint(cc.p(0,0));
var itemTestEventEndpoint = cc.MenuItemLabel.create(labelTestEventEndpoint, this.onMenuTestEventEndpointClicked, this);
itemTestEventEndpoint.setPosition(cc.p(winSize.width - (labelTestEventEndpoint.getContentSize().width / 2 + MARGIN), winSize.height - MARGIN - 3 * SPACE));
menuRequest.addChild(itemTestEventEndpoint);
// Test disconnecting basic client
var labelTestClientDisconnect = cc.LabelTTF.create("Disconnect Socket", "Arial", 22);
labelTestClientDisconnect.setAnchorPoint(cc.p(0,0));
var itemClientDisconnect = cc.MenuItemLabel.create(labelTestClientDisconnect, this.onMenuTestClientDisconnectClicked, this);
itemClientDisconnect.setPosition(cc.p(labelTestClientDisconnect.getContentSize().width / 2 + MARGIN, winSize.height - MARGIN - 4 * SPACE));
menuRequest.addChild(itemClientDisconnect);
// Test disconnecting the endpoint '/testpoint'
var labelTestEndpointDisconnect = cc.LabelTTF.create("Disconnect Endpoint", "Arial", 22);
labelTestEndpointDisconnect.setAnchorPoint(cc.p(0,0));
var itemTestEndpointDisconnect = cc.MenuItemLabel.create(labelTestEndpointDisconnect, this.onMenuTestEndpointDisconnectClicked, this);
itemTestEndpointDisconnect.setPosition(cc.p(winSize.width - (labelTestEndpointDisconnect.getContentSize().width / 2 + MARGIN), winSize.height - MARGIN - 4 * SPACE));
menuRequest.addChild(itemTestEndpointDisconnect);
this._sioClientStatus = cc.LabelTTF.create("Not connected...", "Arial", 14);
this._sioClientStatus.setAnchorPoint(cc.p(0, 0));
this._sioClientStatus.setPosition(cc.p(0,winSize.height * .25));
this.addChild(this._sioClientStatus);
// Back Menu
var itemBack = cc.MenuItemFont.create("Back", this.toExtensionsMainLayer, this);
itemBack.setPosition(cc.p(winSize.width - 50, 25));
var menuBack = cc.Menu.create(itemBack);
menuBack.setPosition(cc.p(0, 0));
this.addChild(menuBack);
return true;
},
onExit: function() {
if(this._sioEndpoint) this._sioEndpoint.disconnect();
if(this._sioClient) this._sioClient.disconnect();
this._super();
},
//socket callback for testing
testevent: function(data) {
var msg = this.tag + " says 'testevent' with data: " + data;
this.statusLabel.setString(msg);
cc.log(msg);
},
message: function(data) {
var msg = this.tag + " received message: " + data;
this.statusLabel.setString(msg);
cc.log(msg);
},
disconnection: function() {
var msg = this.tag + " disconnected!";
this.statusLabel.setString(msg);
cc.log(msg);
},
// Menu Callbacks
onMenuSIOClientClicked: function(sender) {
//create a client by using this static method, url does not need to contain the protocol
var sioclient = SocketIO.connect("ws://channon.us:3000", {"force new connection" : true});
//if you need to track multiple sockets it is best to store them with tags in your own array for now
sioclient.tag = "Test Client";
//attaching the status label to the socketio client
//this is only necessary in javascript due to scope within shared event handlers,
//as 'this' will refer to the socketio client
sioclient.statusLabel = this._sioClientStatus;
//register event callbacks
//this is an example of a handler declared inline
sioclient.on("connect", function() {
var msg = sioclient.tag + " Connected!";
this.statusLabel.setString(msg);
cc.log(msg);
sioclient.send(msg);
});
//example of a handler that is shared between multiple clients
sioclient.on("message", this.message);
sioclient.on("echotest", function(data) {
cc.log("echotest 'on' callback fired!");
var msg = this.tag + " says 'echotest' with data: " + data;
this.statusLabel.setString(msg);
cc.log(msg);
});
sioclient.on("testevent", this.testevent);
sioclient.on("disconnect", this.disconnection);
this._sioClient = sioclient;
},
onMenuSIOEndpointClicked: function(sender) {
//repeat the same connection steps for the namespace "testpoint"
var sioendpoint = SocketIO.connect("ws://channon.us:3000/testpoint");
//a tag to differentiate in shared callbacks
sioendpoint.tag = "Test Endpoint";
sioendpoint.statusLabel = this._sioClientStatus;
sioendpoint.on("connect", function() {
var msg = sioendpoint.tag + " Connected!";
this.statusLabel.setString(msg);
cc.log(msg);
sioendpoint.send(msg);
});
//register event callbacks
sioendpoint.on("echotest", function(data) {
cc.log("echotest 'on' callback fired!");
var msg = this.tag + " says 'echotest' with data: " + data;
this.statusLabel.setString(msg);
cc.log(msg);
});
sioendpoint.on("message", this.message);
//demonstrating how callbacks can be shared within a delegate
sioendpoint.on("testevent", this.testevent);
sioendpoint.on("disconnect", this.disconnection);
this._sioEndpoint = sioendpoint;
},
onMenuTestMessageClicked: function(sender) {
//check that the socket is != NULL before sending or emitting events
//the client should be NULL either before initialization and connection or after disconnect
if(this._sioClient != null) this._sioClient.send("Hello Socket.IO!");
},
onMenuTestMessageEndpointClicked: function(sender) {
if(this._sioEndpoint != null) this._sioEndpoint.send("Hello Socket.IO!");
},
onMenuTestEventClicked: function(sender) {
if(this._sioClient != null) this._sioClient.emit("echotest","[{\"name\":\"myname\",\"type\":\"mytype\"}]");
},
onMenuTestEventEndpointClicked: function(sender) {
if(this._sioEndpoint != null) this._sioEndpoint.emit("echotest","[{\"name\":\"myname\",\"type\":\"mytype\"}]");
},
onMenuTestClientDisconnectClicked: function(sender) {
if(this._sioClient != null) this._sioClient.disconnect();
},
onMenuTestEndpointDisconnectClicked: function(sender) {
if(this._sioEndpoint != null) this._sioEndpoint.disconnect();
},
toExtensionsMainLayer: function (sender) {
var scene = new ExtensionsTestScene();
scene.runThisTest();
}
});
SocketIOTestLayer.create = function () {
var retObj = new SocketIOTestLayer();
if (retObj && retObj.init()) {
return retObj;
}
return null;
};
var runSocketIOTest = function () {
var pScene = cc.Scene.create();
var pLayer = SocketIOTestLayer.create();
pScene.addChild(pLayer);
cc.Director.getInstance().replaceScene(pScene);
};