-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromesocks.py
168 lines (118 loc) · 4.5 KB
/
chromesocks.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python
"""
this looks for data, then stuffs the values into
JSON and pushes the JSON to a web app using websockets.
This needs twisted svn branch
http://twistedmatrix.com/trac/browser/branches/websocket-4173?rev=27790
stomper examples : http://stomper.googlecode.com/svn/trunk/lib/stomper/examples/receiver.py
"""
from twisted.internet import reactor, interfaces, protocol
from twisted.protocols import basic
from twisted.web import server, resource
from twisted.web.static import File
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet.protocol import Factory
from twisted.internet import reactor
from twisted.web.websocket import WebSocketHandler, WebSocketSite
from twisted.web.server import Site
from twisted.web.static import File
from twisted.internet.task import LoopingCall
from twisted.python import log
import sys
import simplejson
import logging
#start the ws blasting thing
instances = []
class Echohandler(WebSocketHandler):
def frameReceived(self, frame):
log.msg("Received frame '%s'" % frame)
self.transport.write(frame + "\n")
def echo_factory(request):
instance = Echohandler(request)
instances.append(instance)
#the instances need to be removed when a client disconnects
#request.notifyFinish().addCallback(lambda x: instances.remove(instance))
return instance
def socket_blast(val):
"""send the data to all open websockets"""
for instance in instances:
instance.transport.write(val + "\n")
###################stomp#########################
from twisted.internet.protocol import Protocol, ReconnectingClientFactory
import stomper
stomper.utils.log_init(logging.DEBUG)
DESTINATION="/topic/test"
class MyStomp(stomper.Engine):
def __init__(self, username='', password=''):
super(MyStomp, self).__init__()
self.username = username
self.password = password
self.log = logging.getLogger("receiver")
def connect(self):
"""Generate the STOMP connect command to get a session.
"""
return stomper.connect(self.username, self.password)
def connected(self, msg):
"""Once I've connected I want to subscribe to my the message queue.
"""
super(MyStomp, self).connected(msg)
self.log.info("connected: session %s" % msg['headers']['session'])
f = stomper.Frame()
f.unpack(stomper.subscribe(DESTINATION))
return f.pack()
def ack(self, msg):
"""Process the message and determine what to do with it.
"""
self.log.info("RECEIVER - received: %s " % msg['body'])
print msg['body']
socket_blast(msg['body'])
print(msg['body'])
# return super(MyStomp, self).ack(msg)
return stomper.NO_REPONSE_NEEDED
class StompProtocol(Protocol):
def __init__(self, username='', password=''):
self.sm = MyStomp(username, password)
def connectionMade(self):
"""Register with the stomp server.
"""
cmd = self.sm.connect()
self.transport.write(cmd)
def dataReceived(self, data):
"""Data received, react to it and respond if needed.
"""
msg = stomper.unpack_frame(data)
returned = self.sm.react(msg)
if returned:
self.transport.write(returned)
class StompClientFactory(ReconnectingClientFactory):
# Will be set up before the factory is created.
username, password = '', ''
def startedConnecting(self, connector):
"""Started to connect.
"""
def buildProtocol(self, addr):
"""Transport level connected now create the communication protocol.
"""
return StompProtocol(self.username, self.password)
def clientConnectionLost(self, connector, reason):
"""Lost connection
"""
print 'Lost connection. Reason:', reason
def clientConnectionFailed(self, connector, reason):
"""Connection failed
"""
print 'Connection failed. Reason:', reason
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
################################################end stomp
def main():
log.startLogging(sys.stdout)
root = File(".")
site = WebSocketSite(root)
site.addHandler("/ws/echo", echo_factory)
reactor.listenTCP(8080, site)
#listen to stomp
reactor.connectTCP('localhost', 61613, StompClientFactory())
reactor.run()
if __name__ == "__main__":
main()