parent
65f2150566
commit
f8aa7c60e7
7 changed files with 110 additions and 142 deletions
@ -0,0 +1,50 @@ |
||||
from tornado import web, ioloop, options, websocket |
||||
import os.path |
||||
|
||||
clients = [] |
||||
messages = [] |
||||
|
||||
class Application(web.Application): |
||||
def __init__(self): |
||||
handlers = [ |
||||
(r"/", MainHandler), |
||||
(r"/chat", ChatHandler), |
||||
] |
||||
settings = dict( |
||||
cookie_secret="tes12323hhdfdfre9312313", |
||||
template_path=os.path.join(os.path.dirname(__file__), "templates"), |
||||
static_path=os.path.join(os.path.dirname(__file__), "static"), |
||||
xsrf_cookies=True, |
||||
) |
||||
super().__init__(handlers, **settings) |
||||
|
||||
|
||||
class MainHandler(web.RequestHandler): |
||||
def get(self): |
||||
self.render("index.html") |
||||
|
||||
|
||||
class ChatHandler(websocket.WebSocketHandler): |
||||
|
||||
def open(self): |
||||
print("Web socket opened") |
||||
clients.append(self) |
||||
|
||||
def on_message(self, message): |
||||
messages.append({'msg':message}) |
||||
for msg in messages: |
||||
self.write_message(msg) |
||||
|
||||
def check_origin(self, origin): |
||||
return True |
||||
|
||||
|
||||
def main(): |
||||
options.parse_command_line() |
||||
app = Application() |
||||
app.listen(8888) |
||||
ioloop.IOLoop.current().start() |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
main() |
||||
@ -0,0 +1,19 @@ |
||||
import logging |
||||
import datetime |
||||
from redis import StrictRedis |
||||
from tornado import web, websocket, escape |
||||
|
||||
r = StrictRedis(db=1) |
||||
|
||||
logger = logging.getLogger('handlers') |
||||
|
||||
|
||||
class ChatHandler(websocket.WebSocketHandler): |
||||
|
||||
waiters = set() |
||||
|
||||
def open(self, *args, **kwargs): |
||||
self.chnl = kwargs.get('channel', 'main') |
||||
self.waiters.add((self.chn1, self)) |
||||
self.chnl_key = 'channels:{}' |
||||
|
||||
@ -0,0 +1,31 @@ |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<title>Title</title> |
||||
</head> |
||||
<body> |
||||
<h1>Всем привет , дети мои!!</h1> |
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> |
||||
<script type="text/javascript"> |
||||
$(document).ready(function(){ |
||||
var url = 'ws://127.0.0.1:8888/chat'; |
||||
var socket = new WebSocket(url); |
||||
|
||||
socket.onopen = function(){ |
||||
console.log("Соединение установлено"); |
||||
socket.send("start"); |
||||
} |
||||
|
||||
socket.onmessage = function (event) { |
||||
console.log(event.data); |
||||
alert(event.data); |
||||
var data = JSON.parse(event.data); |
||||
console.log(data); |
||||
} |
||||
}); |
||||
|
||||
</script> |
||||
</body> |
||||
</html> |
||||
Loading…
Reference in new issue