forked from Pushjet/Pushjet-Connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverWebSocket.py
executable file
·54 lines (40 loc) · 2.16 KB
/
serverWebSocket.py
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
#!/usr/bin/env python2.7
from argparse import ArgumentParser
from sys import stdout
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from protocolBase import PushjetProtocolBase
from twisted.python import log
from twisted.internet import reactor
class PushjetWebSocketBase(WebSocketServerProtocol, PushjetProtocolBase):
def __init__(self):
PushjetProtocolBase.__init__(self, args.api, args.pub)
WebSocketServerProtocol.__init__(self)
self.onMessage = self.onClientMessage
def onConnect(self, request):
print "New connection:", request.peer
def sendMessage(self, payload, **kwargs):
return super(WebSocketServerProtocol, self).sendMessage(self.toAscii(payload), **kwargs)
if __name__ == '__main__':
parser = ArgumentParser(description='Pushjet websocket server')
parser.add_argument('--host', '-u', default='127.0.0.1', type=str,
help='the host the server should bind to (default: 127.0.0.1, this is sane)')
parser.add_argument('--port', '-p', default='8181', type=int,
help='the port the server should bind to (default: 8181)')
parser.add_argument('--api', '-a', default='https://api.pushjet.io', type=str, metavar='SRV',
help='the api server url (default: https://api.pushjet.io)')
parser.add_argument('--pub', '-z', default='ipc:///tmp/pushjet-publisher.ipc', type=str,
help='the publisher uri for receiving messages (default: ipc:///tmp/pushjet-publisher.ipc)')
parser.add_argument('--log', '-l', default=None, type=str, metavar='PATH', dest='logfile',
help='log file path')
parser.add_argument('--quiet', '-q', action='store_true')
args = parser.parse_args()
if not args.quiet:
log.startLogging(stdout)
if args.logfile:
log.startLogging(open(args.logfile, 'a'))
print("Started logging to file %s" % args.logfile)
wsUri = 'ws://%s:%i' % (args.host, args.port)
factory = WebSocketServerFactory(wsUri, debug=False)
factory.protocol = PushjetWebSocketBase
reactor.listenTCP(args.port, factory)
reactor.run()