-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (67 loc) · 2.1 KB
/
index.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
"use strict";
/*!
* Realtime Service
* Copyright(c) 2013 Owen Barnes <[email protected]>
* MIT Licensed
*/
/**
* Load objects to be passed to server() and client()
*/
var Server = require('./server');
var Client = require('realtime-service-client');
function Service (definition, assigned, services) {
if (typeof assigned.id === 'undefined') throw new Error("Service must have an ID");
if (!assigned.name) throw new Error("Service must have a name");
this.assigned = assigned;
this.services = services;
this.use = definition.use || {};
this.serverApi = definition.server;
this.clientApi = definition.client;
this.private = definition.private || false;
this.msgAttrs = [];
if (this.use.callbacks) this.msgAttrs.push('callbackId');
}
Service.prototype.start = function(transport) {
this.server = new Server(this, transport);
return this.serverApi(this.server);
};
Service.prototype.paramsForClient = function() {
return {
id: this.assigned.id,
name: this.assigned.name,
options: this.assigned.options,
use: this.use
};
};
Service.prototype.relativeRoot = function() {
var len = this.services.root.length;
return '.' + this.assigned.root.substr(len);
};
Service.prototype.log = function(args) {
if (this.assigned.log) {
args.unshift(this.assigned.name); // append service name
this.assigned.log.apply(this.assigned.log, args);
}
};
// A mock client used for testing - lot of work and thinking still to do here!
Service.prototype.testClient = function() {
var self = this;
var client = new Client();
var serverTransport = {
sendToSocketId: function(fake, serviceId, msg) {
client.processIncomingMessage(self.assigned.id, msg);
}
};
var clientTransport = {
write: function(serviceId, msg) {
var meta = {transport: 'fakedForTest'};
server.read(msg, meta);
}
};
var server = new Server(this, serverTransport);
var clientApi = client.register(this.paramsForClient(), this.clientApi);
client.connect(clientTransport);
this.serverApi(server);
return clientApi;
};
module.exports = Service;