-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket-interface.js
More file actions
73 lines (66 loc) · 2.58 KB
/
Copy pathwebsocket-interface.js
File metadata and controls
73 lines (66 loc) · 2.58 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
/**
* New node file
*/
var txHandler = require('./transaction-handler');
var ws = require('ws');
var config = require("./config");
// {"commandType" : "openTransaction" } --> {"status": 0 ,"responseCode": 0,"description" : "OK,go on" , txId : "uuid" }
// {"commandType" : "commitTransaction", "txId" : "uuid" } --> {"status": 0 ,"responseCode": 0, "description" : "OK,go on"}
// {"commandType" : "rollbackTransaction", "txId" : "uuid" } --> {"status": 0 ,"responseCode": 0, "description" : "OK,go on"}
// {"commandType" : "execute", "txId" : "uuid" } --> {"status": 0 ,"responseCode": 0, "description" : "OK,go on"}
function initWebSocketServer(){
var WebSocketServer = ws.Server
, wss = new WebSocketServer({port: config.websocketPort});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('server received: %s', message);
cmd = JSON.parse(message);
var txInstance = txHandler.getInstance()
if (cmd.commandType == 'openTransaction') {
txInstance.openTransaction(function() {
var response = initializeSuccesfulResponse();
response.txId = this.getTransactionId() + '';
ws.send(JSON.stringify(response));
console.log('resp : %s', JSON.stringify(response));
},
function(err) {
var response = initializeErrorResponse(err, 1);
ws.send(JSON.stringify(response));
});
} else if (cmd.commandType == 'commitTransaction') {
txInstance.commitTransaction(cmd.txId, function() {
var response = initializeSuccesfulResponse();
ws.send(JSON.stringify(response));
},function(err) {
var response = initializeErrorResponse(err, 1);
ws.send(JSON.stringify(response));
});
} else if (cmd.commandType == 'rollbackTransaction') {
txInstance.commitTransaction(cmd.txId, function() {
var response = initializeSuccesfulResponse();
ws.send(JSON.stringify(response));
},function(err) {
var response = initializeErrorResponse(err, 1);
ws.send(JSON.stringify(response));
});
} else if (cmd.commandType == 'execute') {
txInstance.execute(cmd.cql, cmd.txId, function() {
var response = initializeSuccesfulResponse();
ws.send(JSON.stringify(response));
},function(err) {
var response = initializeErrorResponse(err, 1);
ws.send(JSON.stringify(response));
});
}
});
});
}
function initializeSuccesfulResponse(){
return {status:0,responseCode:0,description:"OK"};
}
function initializeErrorResponse(err,code){
return {status:1,responseCode:code,description:err.message,stack:err.stack};
}
module.exports = {
initWebSocketServer : initWebSocketServer
}