-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.tcpServer.js
92 lines (77 loc) · 2.93 KB
/
sockets.tcpServer.js
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
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var Event = require('cordova-plugin-chrome-apps-common.events');
var exec = cordova.require('cordova/exec');
var callbackWithError = require('cordova-plugin-chrome-apps-common.errors').callbackWithError;
exports.create = function(properties /** optional */, callback) {
if (typeof properties == 'function') {
callback = properties;
properties = {};
}
var win = callback && function(socketId) {
var createInfo = {
socketId: socketId
};
callback(createInfo);
};
exec(win, null, 'ChromeSocketsTcpServer', 'create', [properties]);
};
exports.update = function(socketId, properties, callback) {
exec(callback, null, 'ChromeSocketsTcpServer', 'update', [socketId, properties]);
};
exports.setPaused = function(socketId, paused, callback) {
exec(callback, null, 'ChromeSocketsTcpServer', 'setPaused', [socketId, paused]);
};
exports.listen = function(socketId, address, port, backlog /** optional */, callback) {
if (typeof backlog == 'function') {
callback = backlog;
backlog = null; /** Use SOMAXCONN */
}
var win = callback && function() {
callback(0);
};
var fail = callback && function(error) {
callbackWithError(error.message, callback, error.resultCode);
};
exec(win, fail, 'ChromeSocketsTcpServer', 'listen', [socketId, address, port, backlog]);
};
exports.disconnect = function(socketId, callback) {
exec(callback, null, 'ChromeSocketsTcpServer', 'disconnect', [socketId]);
};
exports.close = function(socketId, callback) {
exec(callback, null, 'ChromeSocketsTcpServer', 'close', [socketId]);
};
exports.getInfo = function(socketId, callback) {
var win = callback && function(result) {
result.persistent = !!result.persistent;
result.paused = !!result.paused;
callback(result);
};
exec(win, null, 'ChromeSocketsTcpServer', 'getInfo', [socketId]);
};
exports.getSockets = function(callback) {
var win = callback && function(results) {
for (var result in results) {
result.persistent = !!result.persistent;
result.paused = !!result.paused;
}
callback(results);
};
exec(win, null, 'ChromeSocketsTcpServer', 'getSockets', []);
};
exports.onAccept = new Event('onAccept');
exports.onAcceptError = new Event('onAcceptError');
function registerAcceptEvents() {
var win = function(info) {
exports.onAccept.fire(info);
};
var fail = function(info) {
var error = function() {
exports.onAcceptError.fire(info);
};
callbackWithError(info.message, error);
};
exec(win, fail, 'ChromeSocketsTcpServer', 'registerAcceptEvents', []);
}
require('cordova-plugin-chrome-apps-common.helpers').runAtStartUp(registerAcceptEvents);