FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rabbitmqexample/webSocketPython/my-server.py at master · imperialguy/rabbitmqexample · GitHub
imperialguy
/
rabbitmqexample
Public
forked from
Gsantomaggio/rabbitmqexample
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rabbitmqexample
/
webSocketPython
/
my-server.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
93 lines (69 loc) · 2.08 KB
Breadcrumbs
rabbitmqexample
/
webSocketPython
/
my-server.py
Copy path
File metadata and controls
executable file
·
93 lines (69 loc) · 2.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
# How to send an web socket message using http://www.tornadoweb.org/.
# to get ready you have to install pika and tornado
# 1. pip install pika
# 2. pip install tornado
import
os
import
tornado
.
ioloop
import
tornado
.
web
import
tornado
.
websocket
import
pika
from
threading
import
Thread
import
logging
logging
.
basicConfig
(
level
=
logging
.
INFO
)
# web socket clients connected.
clients
=
[]
connection
=
pika
.
BlockingConnection
()
logging
.
info
(
'Connected:localhost'
)
channel
=
connection
.
channel
()
def
threaded_rmq
():
channel
.
queue_declare
(
queue
=
"my_queue"
)
logging
.
info
(
'consumer ready, on my_queue'
)
channel
.
basic_consume
(
consumer_callback
,
queue
=
"my_queue"
,
no_ack
=
True
)
channel
.
start_consuming
()
def
disconnect_to_rabbitmq
():
channel
.
stop_consuming
()
connection
.
close
()
logging
.
info
(
'Disconnected from Rabbitmq'
)
def
consumer_callback
(
ch
,
method
,
properties
,
body
):
logging
.
info
(
"[x] Received %r"
%
(
body
,))
# The messagge is brodcast to the connected clients
for
itm
in
clients
:
itm
.
write_message
(
body
)
class
SocketHandler
(
tornado
.
websocket
.
WebSocketHandler
):
def
open
(
self
):
logging
.
info
(
'WebSocket opened'
)
clients
.
append
(
self
)
def
on_close
(
self
):
logging
.
info
(
'WebSocket closed'
)
clients
.
remove
(
self
)
class
MainHandler
(
tornado
.
web
.
RequestHandler
):
def
get
(
self
):
self
.
render
(
"websocket.html"
)
application
=
tornado
.
web
.
Application
([
(
r'/ws'
,
SocketHandler
),
(
r"/"
,
MainHandler
),
])
def
startTornado
():
application
.
listen
(
8888
)
tornado
.
ioloop
.
IOLoop
.
instance
().
start
()
def
stopTornado
():
tornado
.
ioloop
.
IOLoop
.
instance
().
stop
()
if
__name__
==
"__main__"
:
logging
.
info
(
'Starting thread RabbitMQ'
)
threadRMQ
=
Thread
(
target
=
threaded_rmq
)
threadRMQ
.
start
()
logging
.
info
(
'Starting thread Tornado'
)
threadTornado
=
Thread
(
target
=
startTornado
)
threadTornado
.
start
()
try
:
raw_input
(
"Server ready. Press enter to stop
\n
"
)
except
SyntaxError
:
pass
try
:
logging
.
info
(
'Disconnecting from RabbitMQ..'
)
disconnect_to_rabbitmq
()
except
Exception
,
e
:
pass
stopTornado
();
logging
.
info
(
'See you...'
)
Back
|
FazBrowse Home
|
New Git URL