UNPKG

1.27 kBJavaScriptView Raw
1var Frame = require('./frame')
2 , Hand = require('./hand')
3 , Pointable = require('./pointable')
4 , Finger = require('./finger');
5
6var Event = function(data) {
7 this.type = data.type;
8 this.state = data.state;
9};
10
11exports.chooseProtocol = function(header) {
12 var protocol;
13 switch(header.version) {
14 case 1:
15 case 2:
16 case 3:
17 case 4:
18 case 5:
19 case 6:
20 protocol = JSONProtocol(header);
21 protocol.sendBackground = function(connection, state) {
22 connection.send(protocol.encode({background: state}));
23 }
24 protocol.sendFocused = function(connection, state) {
25 connection.send(protocol.encode({focused: state}));
26 }
27 break;
28 default:
29 throw "unrecognized version";
30 }
31 return protocol;
32}
33
34var JSONProtocol = exports.JSONProtocol = function(header) {
35 var protocol = function(data) {
36 if (data.event) {
37 return new Event(data.event);
38 } else {
39 var frame = new Frame(data);
40 return frame;
41 }
42 };
43
44 protocol.encode = function(message) {
45 return JSON.stringify(message);
46 }
47 protocol.version = header.version;
48 protocol.serviceVersion = header.serviceVersion;
49 protocol.versionLong = 'Version ' + header.version;
50 protocol.type = 'protocol';
51 return protocol;
52};