forked from mqttjs/MQTT.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (81 loc) · 2.04 KB
/
server.js
File metadata and controls
94 lines (81 loc) · 2.04 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict'
const net = require('net')
const tls = require('tls')
const Connection = require('mqtt-connection')
/**
* MqttServer
*
* @param {Function} listener - fired on client connection
*/
class MqttServer extends net.Server {
constructor (listener) {
super()
this.connectionList = []
const that = this
this.on('connection', function (duplex) {
this.connectionList.push(duplex)
const connection = new Connection(duplex, function () {
that.emit('client', connection)
})
})
if (listener) {
this.on('client', listener)
}
}
}
/**
* MqttServerNoWait (w/o waiting for initialization)
*
* @param {Function} listener - fired on client connection
*/
class MqttServerNoWait extends net.Server {
constructor (listener) {
super()
this.connectionList = []
this.on('connection', function (duplex) {
this.connectionList.push(duplex)
const connection = new Connection(duplex)
// do not wait for connection to return to send it to the client.
this.emit('client', connection)
})
if (listener) {
this.on('client', listener)
}
}
}
/**
* MqttSecureServer
*
* @param {Object} opts - server options
* @param {Function} listener
*/
class MqttSecureServer extends tls.Server {
constructor (opts, listener) {
if (typeof opts === 'function') {
listener = opts
opts = {}
}
// sets a listener for the 'connection' event
super(opts)
this.connectionList = []
this.on('secureConnection', function (socket) {
this.connectionList.push(socket)
const that = this
const connection = new Connection(socket, function () {
that.emit('client', connection)
})
})
if (listener) {
this.on('client', listener)
}
}
setupConnection (duplex) {
const that = this
const connection = new Connection(duplex, function () {
that.emit('client', connection)
})
}
}
exports.MqttServer = MqttServer
exports.MqttServerNoWait = MqttServerNoWait
exports.MqttSecureServer = MqttSecureServer