Simon Hunt

GUI -- Created Mock WebSocket Server (runs with node.js)

- moved test scenario events to directory in test branch.

Change-Id: I1f94a9b4c844be1ea2633de0c74d49ebf5a770a7
Showing 243 changed files with 496 additions and 442 deletions
...@@ -30,7 +30,8 @@ ...@@ -30,7 +30,8 @@
30 // creates a web socket for the given path, returning a "handle". 30 // creates a web socket for the given path, returning a "handle".
31 // cb is the callbacks block. 31 // cb is the callbacks block.
32 function createWebSocket(path, cb) { 32 function createWebSocket(path, cb) {
33 - var fullUrl = ufs.wsUrl(path), 33 + //var fullUrl = ufs.wsUrl(path),
34 + var fullUrl = 'ws://localhost:8123/foo',
34 ws = new WebSocket(fullUrl), 35 ws = new WebSocket(fullUrl),
35 api = { 36 api = {
36 meta: { path: fullUrl, ws: ws }, 37 meta: { path: fullUrl, ws: ws },
......
1 -{
2 - "event": "showTraffic",
3 - "sid": 1,
4 - "payload": {
5 - "paths": [
6 - {
7 - "class": "secondary",
8 - "links": [
9 - "0E:2A:69:30:13:86/-1/0-of:0000ffffffff0003/101",
10 - "0E:2A:69:30:13:aa/-1/0-of:0000ffffffff0008/101"
11 - ],
12 - "labels": [
13 - "35 bytes",
14 - "35 bytes"
15 - ]
16 - },
17 - {
18 - "class": "secondary optical",
19 - "links": [
20 - "of:0000ffffffff0003/4-of:0000ffffffffff03/1",
21 - "of:0000ffffffff0008/4-of:0000ffffffffff08/1",
22 - "of:0000ffffffffff08/4-of:0000ffffffffff03/1"
23 - ],
24 - "labels": [
25 - "24 bytes",
26 - "some bits",
27 - "foo bars"
28 - ]
29 - }
30 - ]
31 - }
32 -}
1 -{
2 - "event": "showTraffic",
3 - "sid": 1,
4 - "payload": {
5 - "paths": [
6 - {
7 - "class": "animated",
8 - "traffic": true,
9 - "links": [
10 - "0E:2A:69:30:13:86/-1/0-of:0000ffffffff0003/101",
11 - "0E:2A:69:30:13:aa/-1/0-of:0000ffffffff0008/101"
12 - ],
13 - "labels": [
14 - "47 bytes",
15 - "47 bytes"
16 - ]
17 - },
18 - {
19 - "class": "animated optical",
20 - "traffic": true,
21 - "links": [
22 - "of:0000ffffffff0003/4-of:0000ffffffffff03/1",
23 - "of:0000ffffffff0008/4-of:0000ffffffffff08/1",
24 - "of:0000ffffffffff08/4-of:0000ffffffffff03/1"
25 - ],
26 - "labels": [
27 - "24 bytes",
28 - "some bits",
29 - "foo bars"
30 - ]
31 - }
32 - ]
33 - }
34 -}
1 -{
2 - "event": "showTraffic",
3 - "sid": 1,
4 - "payload": {
5 - "paths": [
6 - {
7 - "class": "primary",
8 - "traffic": false,
9 - "links": [
10 - "of:0000ffffffff0008/2-of:0000ffffffff0003/1"
11 - ],
12 - "labels": [""]
13 - },
14 - {
15 - "class": "secondary",
16 - "traffic": false,
17 - "links": [
18 - "of:0000ffffffff0003/9-of:0000ffffffff0007/2"
19 - ],
20 - "labels": [""]
21 - },
22 - {
23 - "class": "animated",
24 - "traffic": true,
25 - "links": [
26 - "of:0000ffffffff0008/4-of:0000ffffffff0007/1"
27 - ],
28 - "labels": [""]
29 - },
30 - {
31 - "class": "animated optical",
32 - "traffic": true,
33 - "links": [
34 - "of:0000ffffffff0008/4-of:0000ffffffffff08/1"
35 - ],
36 - "labels": [""]
37 - },
38 - {
39 - "class": "secondary optical",
40 - "traffic": false,
41 - "links": [
42 - "of:0000ffffffff0003/4-of:0000ffffffffff03/1"
43 - ],
44 - "labels": [""]
45 - },
46 - {
47 - "class": "primary optical",
48 - "traffic": false,
49 - "links": [
50 - "of:0000ffffffffff08/4-of:0000ffffffffff03/1"
51 - ],
52 - "labels": [""]
53 - }
54 - ]
55 - }
56 -}
1 -{
2 - "event": "showTraffic",
3 - "sid": 1,
4 - "payload": {
5 - "paths": []
6 - }
7 -}
1 -{
2 - "event": "addDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0008",
5 - "type": "switch",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "sw-8",
10 - "0000ffffffff0008"
11 - ],
12 - "metaUi": {
13 - "x": 734,
14 - "y": 477
15 - }
16 - }
17 -}
1 -{
2 - "event": "updateDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0007",
5 - "type": "switch",
6 - "online": false,
7 - "labels": [
8 - "",
9 - "sw-7",
10 - "0000ffffffff0007"
11 - ],
12 - "metaUi": {
13 - "x": 530,
14 - "y": 330
15 - }
16 - }
17 -}
1 -{
2 - "event": "updateDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0007",
5 - "type": "switch",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "sw-7",
10 - "0000ffffffff0007"
11 - ],
12 - "metaUi": {
13 - "x": 530,
14 - "y": 330
15 - }
16 - }
17 -}
1 -{
2 - "event": "removeDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0008",
5 - "type": "switch",
6 - "online": false,
7 - "labels": [
8 - "",
9 - "sw-8",
10 - "0000ffffffff0008"
11 - ],
12 - "metaUi": {
13 - "x": 734,
14 - "y": 477
15 - }
16 - }
17 -}
1 -{
2 - "event": "addDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0008",
5 - "type": "switch",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "sw-8",
10 - "0000ffffffff0008"
11 - ],
12 - "metaUi": {
13 - "x": 734,
14 - "y": 477
15 - }
16 - }
17 -}
1 -{
2 - "event": "removeHost",
3 - "payload": {
4 - "id": "0E:2A:69:30:13:88/-1",
5 - "ingress": "0E:2A:69:30:13:88/-1/0-of:0000ffffffff0007/101",
6 - "egress": "of:0000ffffffff0007/101-0E:2A:69:30:13:86/-1/0",
7 - "cp": {
8 - "device": "of:0000ffffffff0007",
9 - "port": 101
10 - },
11 - "labels": [
12 - "4.5.7.6",
13 - "0E:2A:69:30:13:88"
14 - ],
15 - "props": {}
16 - }
17 -}
1 -{
2 - "event": "addDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0003",
5 - "type": "switch",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "sw-3",
10 - "0000ffffffff0003"
11 - ],
12 - "metaUi": {
13 - "x": 282,
14 - "y": 503
15 - }
16 - }
17 -}
1 -{
2 - "event": "addDevice",
3 - "payload": {
4 - "id": "of:0000ffffffff0007",
5 - "type": "switch",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "sw-7",
10 - "0000ffffffff0007"
11 - ],
12 - "metaUi": {
13 - "x": 530,
14 - "y": 330
15 - }
16 - }
17 -}
1 -{
2 - "event": "addDevice",
3 - "payload": {
4 - "id": "of:0000ffffffffff08",
5 - "type": "roadm",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "opt-8",
10 - "0000ffffffffff08"
11 - ],
12 - "metaUi": {
13 - "x": 734,
14 - "y": 577
15 - }
16 - }
17 -}
1 -{
2 - "event": "addDevice",
3 - "payload": {
4 - "id": "of:0000ffffffffff03",
5 - "type": "roadm",
6 - "online": true,
7 - "labels": [
8 - "",
9 - "opt-3",
10 - "0000ffffffffff03"
11 - ],
12 - "metaUi": {
13 - "x": 282,
14 - "y": 603
15 - }
16 - }
17 -}
1 -{
2 - "event": "addLink",
3 - "payload": {
4 - "id": "of:0000ffffffff0003/9-of:0000ffffffff0007/2",
5 - "type": "direct",
6 - "online": true,
7 - "linkWidth": 2,
8 - "src": "of:0000ffffffff0003",
9 - "srcPort": "9",
10 - "dst": "of:0000ffffffff0007",
11 - "dstPort": "2",
12 - "props" : {
13 - "BW": "120 Gb"
14 - }
15 - }
16 -}
1 -{
2 - "event": "addLink",
3 - "payload": {
4 - "id": "of:0000ffffffff0008/2-of:0000ffffffff0003/1",
5 - "type": "direct",
6 - "online": true,
7 - "linkWidth": 2,
8 - "src": "of:0000ffffffff0008",
9 - "srcPort": "2",
10 - "dst": "of:0000ffffffff0003",
11 - "dstPort": "1",
12 - "props" : {
13 - "BW": "70 Gb"
14 - }
15 - }
16 -}
1 -{
2 - "event": "addLink",
3 - "payload": {
4 - "id": "of:0000ffffffff0008/4-of:0000ffffffff0007/1",
5 - "type": "direct",
6 - "online": true,
7 - "linkWidth": 2,
8 - "src": "of:0000ffffffff0008",
9 - "srcPort": "4",
10 - "dst": "of:0000ffffffff0007",
11 - "dstPort": "1",
12 - "props" : {
13 - "BW": "90 Gb"
14 - }
15 - }
16 -}
1 -{
2 - "event": "addLink",
3 - "payload": {
4 - "id": "of:0000ffffffff0008/4-of:0000ffffffffff08/1",
5 - "type": "direct",
6 - "online": true,
7 - "linkWidth": 2,
8 - "src": "of:0000ffffffff0008",
9 - "srcPort": "4",
10 - "dst": "of:0000ffffffffff08",
11 - "dstPort": "1",
12 - "props" : {
13 - "BW": "90 Gb"
14 - }
15 - }
16 -}
1 -{
2 - "comments": [
3 - "Stepping through showTraffic"
4 - ],
5 - "title": "Show Traffic Scenario",
6 - "params": {
7 - "lastAuto": 14
8 - },
9 - "description": [
10 - "Figure out primary, secondary and animated link visualizations.",
11 - "",
12 - "Press 'H' to show hosts.",
13 - "",
14 - "Press '=' to load initial events.",
15 - "Press '-' to fire further events."
16 - ]
17 -}
1 -see: http://bost.ocks.org/mike/map/
2 -
3 -brew install gdal
4 -npm install -g topojson
5 -
6 -To generate continental US map:
7 -
8 - $ wget 'http://www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_1_states_provinces_lakes.zip'
9 - $ unzip ne_50m_admin_1_states_provinces_lakes.zip
10 - $ ogr2ogr -f GeoJSON -where "sr_adm0_a3 IN ('USA')" states.json ne_50m_admin_1_states_provinces_lakes.shp
11 -
12 -edit states.json to remove data for Hawaii and Alaska
13 -
14 - $ topojson states.json > topology.json
15 -
16 -
17 -The .shp file above is incomplete (USA and part of Candada.)
18 -So it may be that each region requires a bit of research to generate.
19 -Ideally a source for public domain shp files can be found that covers all geographic regions.
20 -
21 -
22 -For Canada:
23 -
24 - # wget 'http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gpr_000b11a_e.zip'
25 - # unzip gpr_000b11a_e.zip
26 - # ogr2ogr -f "GeoJSON" -s_srs EPSG:21781 -t_srs EPSG:4326 canada.json gpr_000b11a_e.shp
27 - # topojson --id-property CFSAUID -p name=PRNAME -p name canada.json > topology.json
28 -
29 -
30 -This produces a very large (5MB) file and draws very slowly in Chrome.
31 -So some additional processing is required to simplify the geometry. (It is not checked in.)
32 -
33 -Also, the specification of object structure within the geojson is unclear.
34 -In the US map the geojson structure is
35 -
36 - json.objects.states
37 -
38 -but in the Canadian data it's
39 -
40 - json.objects.canada
41 -
42 -
43 -Lastly, the projection that is used may be tailored to the region.
44 -The preferred projection for the US is "albers" and d3 provides a "albersUSA" which can be used to
45 - project hawaii and alaska as well
46 -
47 -For Canada, apparantly a "Lambert" projection (called conicConformal in d3) is preferred
48 -
49 -see:
50 - https://github.com/mbostock/d3/wiki/Geo-Projections
51 - http://www.statcan.gc.ca/pub/92-195-x/2011001/other-autre/mapproj-projcarte/m-c-eng.htm
52 -
53 -
54 -Summary:
55 -- some additional work is required to fully generalize maps functionality.
56 -- it may be worthwhile for ON.LAB to provide the topo files for key regions since producing these
57 - files is non-trivial
58 -
59 -
60 -
61 -
This diff could not be displayed because it is too large.
1 +{
2 + "event": "showTraffic",
3 + "sid": 1,
4 + "payload": {
5 + "paths": [
6 + {
7 + "class": "secondary",
8 + "links": [
9 + "0E:2A:69:30:13:86/-1/0-of:0000ffffffff0003/101",
10 + "0E:2A:69:30:13:aa/-1/0-of:0000ffffffff0008/101"
11 + ],
12 + "labels": [
13 + "35 bytes",
14 + "35 bytes"
15 + ]
16 + },
17 + {
18 + "class": "secondary optical",
19 + "links": [
20 + "of:0000ffffffff0003/4-of:0000ffffffffff03/1",
21 + "of:0000ffffffff0008/4-of:0000ffffffffff08/1",
22 + "of:0000ffffffffff08/4-of:0000ffffffffff03/1"
23 + ],
24 + "labels": [
25 + "24 bytes",
26 + "some bits",
27 + "foo bars"
28 + ]
29 + }
30 + ]
31 + }
32 +}
1 +{
2 + "event": "showTraffic",
3 + "sid": 1,
4 + "payload": {
5 + "paths": [
6 + {
7 + "class": "animated",
8 + "traffic": true,
9 + "links": [
10 + "0E:2A:69:30:13:86/-1/0-of:0000ffffffff0003/101",
11 + "0E:2A:69:30:13:aa/-1/0-of:0000ffffffff0008/101"
12 + ],
13 + "labels": [
14 + "47 bytes",
15 + "47 bytes"
16 + ]
17 + },
18 + {
19 + "class": "animated optical",
20 + "traffic": true,
21 + "links": [
22 + "of:0000ffffffff0003/4-of:0000ffffffffff03/1",
23 + "of:0000ffffffff0008/4-of:0000ffffffffff08/1",
24 + "of:0000ffffffffff08/4-of:0000ffffffffff03/1"
25 + ],
26 + "labels": [
27 + "24 bytes",
28 + "some bits",
29 + "foo bars"
30 + ]
31 + }
32 + ]
33 + }
34 +}
1 +{
2 + "event": "showTraffic",
3 + "sid": 1,
4 + "payload": {
5 + "paths": [
6 + {
7 + "class": "primary",
8 + "traffic": false,
9 + "links": [
10 + "of:0000ffffffff0008/2-of:0000ffffffff0003/1"
11 + ],
12 + "labels": [""]
13 + },
14 + {
15 + "class": "secondary",
16 + "traffic": false,
17 + "links": [
18 + "of:0000ffffffff0003/9-of:0000ffffffff0007/2"
19 + ],
20 + "labels": [""]
21 + },
22 + {
23 + "class": "animated",
24 + "traffic": true,
25 + "links": [
26 + "of:0000ffffffff0008/4-of:0000ffffffff0007/1"
27 + ],
28 + "labels": [""]
29 + },
30 + {
31 + "class": "animated optical",
32 + "traffic": true,
33 + "links": [
34 + "of:0000ffffffff0008/4-of:0000ffffffffff08/1"
35 + ],
36 + "labels": [""]
37 + },
38 + {
39 + "class": "secondary optical",
40 + "traffic": false,
41 + "links": [
42 + "of:0000ffffffff0003/4-of:0000ffffffffff03/1"
43 + ],
44 + "labels": [""]
45 + },
46 + {
47 + "class": "primary optical",
48 + "traffic": false,
49 + "links": [
50 + "of:0000ffffffffff08/4-of:0000ffffffffff03/1"
51 + ],
52 + "labels": [""]
53 + }
54 + ]
55 + }
56 +}
1 +{
2 + "event": "showTraffic",
3 + "sid": 1,
4 + "payload": {
5 + "paths": []
6 + }
7 +}
1 +{
2 + "event": "addDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0008",
5 + "type": "switch",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "sw-8",
10 + "0000ffffffff0008"
11 + ],
12 + "metaUi": {
13 + "x": 734,
14 + "y": 477
15 + }
16 + }
17 +}
1 +{
2 + "event": "updateDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0007",
5 + "type": "switch",
6 + "online": false,
7 + "labels": [
8 + "",
9 + "sw-7",
10 + "0000ffffffff0007"
11 + ],
12 + "metaUi": {
13 + "x": 530,
14 + "y": 330
15 + }
16 + }
17 +}
1 +{
2 + "event": "updateDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0007",
5 + "type": "switch",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "sw-7",
10 + "0000ffffffff0007"
11 + ],
12 + "metaUi": {
13 + "x": 530,
14 + "y": 330
15 + }
16 + }
17 +}
1 +{
2 + "event": "removeDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0008",
5 + "type": "switch",
6 + "online": false,
7 + "labels": [
8 + "",
9 + "sw-8",
10 + "0000ffffffff0008"
11 + ],
12 + "metaUi": {
13 + "x": 734,
14 + "y": 477
15 + }
16 + }
17 +}
1 +{
2 + "event": "addDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0008",
5 + "type": "switch",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "sw-8",
10 + "0000ffffffff0008"
11 + ],
12 + "metaUi": {
13 + "x": 734,
14 + "y": 477
15 + }
16 + }
17 +}
1 +{
2 + "event": "removeHost",
3 + "payload": {
4 + "id": "0E:2A:69:30:13:88/-1",
5 + "ingress": "0E:2A:69:30:13:88/-1/0-of:0000ffffffff0007/101",
6 + "egress": "of:0000ffffffff0007/101-0E:2A:69:30:13:86/-1/0",
7 + "cp": {
8 + "device": "of:0000ffffffff0007",
9 + "port": 101
10 + },
11 + "labels": [
12 + "4.5.7.6",
13 + "0E:2A:69:30:13:88"
14 + ],
15 + "props": {}
16 + }
17 +}
1 +{
2 + "event": "addDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0003",
5 + "type": "switch",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "sw-3",
10 + "0000ffffffff0003"
11 + ],
12 + "metaUi": {
13 + "x": 282,
14 + "y": 503
15 + }
16 + }
17 +}
1 +{
2 + "event": "addDevice",
3 + "payload": {
4 + "id": "of:0000ffffffff0007",
5 + "type": "switch",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "sw-7",
10 + "0000ffffffff0007"
11 + ],
12 + "metaUi": {
13 + "x": 530,
14 + "y": 330
15 + }
16 + }
17 +}
1 +{
2 + "event": "addDevice",
3 + "payload": {
4 + "id": "of:0000ffffffffff08",
5 + "type": "roadm",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "opt-8",
10 + "0000ffffffffff08"
11 + ],
12 + "metaUi": {
13 + "x": 734,
14 + "y": 577
15 + }
16 + }
17 +}
1 +{
2 + "event": "addDevice",
3 + "payload": {
4 + "id": "of:0000ffffffffff03",
5 + "type": "roadm",
6 + "online": true,
7 + "labels": [
8 + "",
9 + "opt-3",
10 + "0000ffffffffff03"
11 + ],
12 + "metaUi": {
13 + "x": 282,
14 + "y": 603
15 + }
16 + }
17 +}
1 +{
2 + "event": "addLink",
3 + "payload": {
4 + "id": "of:0000ffffffff0003/9-of:0000ffffffff0007/2",
5 + "type": "direct",
6 + "online": true,
7 + "linkWidth": 2,
8 + "src": "of:0000ffffffff0003",
9 + "srcPort": "9",
10 + "dst": "of:0000ffffffff0007",
11 + "dstPort": "2",
12 + "props" : {
13 + "BW": "120 Gb"
14 + }
15 + }
16 +}
1 +{
2 + "event": "addLink",
3 + "payload": {
4 + "id": "of:0000ffffffff0008/2-of:0000ffffffff0003/1",
5 + "type": "direct",
6 + "online": true,
7 + "linkWidth": 2,
8 + "src": "of:0000ffffffff0008",
9 + "srcPort": "2",
10 + "dst": "of:0000ffffffff0003",
11 + "dstPort": "1",
12 + "props" : {
13 + "BW": "70 Gb"
14 + }
15 + }
16 +}
1 +{
2 + "event": "addLink",
3 + "payload": {
4 + "id": "of:0000ffffffff0008/4-of:0000ffffffff0007/1",
5 + "type": "direct",
6 + "online": true,
7 + "linkWidth": 2,
8 + "src": "of:0000ffffffff0008",
9 + "srcPort": "4",
10 + "dst": "of:0000ffffffff0007",
11 + "dstPort": "1",
12 + "props" : {
13 + "BW": "90 Gb"
14 + }
15 + }
16 +}
1 +{
2 + "event": "addLink",
3 + "payload": {
4 + "id": "of:0000ffffffff0008/4-of:0000ffffffffff08/1",
5 + "type": "direct",
6 + "online": true,
7 + "linkWidth": 2,
8 + "src": "of:0000ffffffff0008",
9 + "srcPort": "4",
10 + "dst": "of:0000ffffffffff08",
11 + "dstPort": "1",
12 + "props" : {
13 + "BW": "90 Gb"
14 + }
15 + }
16 +}
1 +{
2 + "comments": [
3 + "Stepping through showTraffic"
4 + ],
5 + "title": "Show Traffic Scenario",
6 + "params": {
7 + "lastAuto": 14
8 + },
9 + "description": [
10 + "Figure out primary, secondary and animated link visualizations.",
11 + "",
12 + "Press 'H' to show hosts.",
13 + "",
14 + "Press '=' to load initial events.",
15 + "Press '-' to fire further events."
16 + ]
17 +}
1 +#!/usr/bin/env node
2 +// === Mock Web Socket Server - for testing the topology view
3 +//
4 +
5 +var readline = require('readline');
6 +var WebSocketServer = require('websocket').server;
7 +var http = require('http');
8 +var port = 8123;
9 +
10 +
11 +var server = http.createServer(function(request, response) {
12 + console.log((new Date()) + ' Received request for ' + request.url);
13 + response.writeHead(404);
14 + response.end();
15 +});
16 +
17 +server.listen(port, function() {
18 + console.log((new Date()) + ' Server is listening on port ' + port);
19 +});
20 +
21 +server.on('listening', function () {
22 + console.log('ok, server is running');
23 +});
24 +
25 +var wsServer = new WebSocketServer({
26 + httpServer: server,
27 + // You should not use autoAcceptConnections for production
28 + // applications, as it defeats all standard cross-origin protection
29 + // facilities built into the protocol and the browser. You should
30 + // *always* verify the connection's origin and decide whether or not
31 + // to accept it.
32 + autoAcceptConnections: false
33 +});
34 +
35 +function originIsAllowed(origin) {
36 + // put logic here to detect whether the specified origin is allowed.
37 + return true;
38 +}
39 +
40 +var connection;
41 +
42 +wsServer.on('request', function(request) {
43 + console.log(); // newline after prompt
44 + console.log("Origin: ", request.origin);
45 +
46 + if (!originIsAllowed(request.origin)) {
47 + // Make sure we only accept requests from an allowed origin
48 + request.reject();
49 + console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
50 + return;
51 + }
52 +
53 + connection = request.accept(null, request.origin);
54 +
55 +
56 + console.log((new Date()) + ' Connection accepted.');
57 + rl.prompt();
58 +
59 +
60 + connection.on('message', function(message) {
61 + if (message.type === 'utf8') {
62 + console.log(); // newline after prompt
63 + console.log('Received Message: ' + message.utf8Data);
64 + //connection.sendUTF(message.utf8Data);
65 + rl.prompt();
66 + }
67 + else if (message.type === 'binary') {
68 + console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
69 + //connection.sendBytes(message.binaryData);
70 + }
71 + });
72 + connection.on('close', function(reasonCode, description) {
73 + console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
74 + });
75 +});
76 +
77 +
78 +var rl = readline.createInterface(process.stdin, process.stdout);
79 +rl.setPrompt('ws> ');
80 +
81 +setTimeout(doCli, 10);
82 +
83 +function doCli() {
84 + rl.prompt();
85 + rl.on('line', function (line) {
86 + var words = line.trim().split(' '),
87 + cmd = words.shift(),
88 + str = words.join(' ');
89 +
90 + switch(cmd) {
91 + case 'hello':
92 + console.log('hello back: ' + str);
93 + break;
94 +
95 + case 'quit':
96 + process.exit(0);
97 + break;
98 +
99 + case 'm':
100 + console.log('sending message: ' + str);
101 + connection.sendUTF(str);
102 + break;
103 +
104 + default:
105 + console.log('Say what?! [' + line.trim() + ']');
106 + break;
107 + }
108 + rl.prompt();
109 + }).on('close', function () {
110 + console.log('quitting...');
111 + process.exit(0);
112 + });
113 +
114 +}