Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
88 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__author__ = 'masnun' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from tornado.web import RequestHandler | ||
from tornado.template import Loader | ||
from settings import TEMPLATE_PATH | ||
|
||
class WebRequestHandler(RequestHandler): | ||
def initialize(self): | ||
self.template_path = TEMPLATE_PATH | ||
#print TEMPLATE_PATH | ||
|
||
def render(self, template_name, params=None): | ||
loader = Loader(self.template_path) | ||
html = loader.load(template_name).generate(**params) | ||
self.write(html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SOCKETS = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,33 @@ | ||
# Add to path | ||
import sys | ||
import os | ||
|
||
sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
# Bootstrap the app | ||
import tornado.ioloop | ||
import tornado.web | ||
from tornado import websocket | ||
|
||
GLOBALS={ | ||
'sockets': [] | ||
} | ||
|
||
class MainHandler(tornado.web.RequestHandler): | ||
def get(self): | ||
#self.write("STATUS: Server running") | ||
html = open("chat.html","r") | ||
self.write(html.read()) | ||
|
||
class ClientSocket(websocket.WebSocketHandler): | ||
def open(self): | ||
GLOBALS['sockets'].append(self) | ||
print "WebSocket opened" | ||
|
||
def on_close(self): | ||
print "WebSocket closed" | ||
GLOBALS['sockets'].remove(self) | ||
|
||
class Announcer(tornado.web.RequestHandler): | ||
def get(self, *args, **kwargs): | ||
data = self.get_argument('data') | ||
for socket in GLOBALS['sockets']: | ||
socket.write_message(data) | ||
self.write('Posted') | ||
|
||
application = tornado.web.Application([ | ||
(r"/", MainHandler), | ||
(r"/socket", ClientSocket), | ||
(r"/push", Announcer), | ||
]) | ||
import tornado.autoreload | ||
import datetime | ||
from urls import urlpatterns | ||
|
||
|
||
# Server Auto Reload Hook | ||
|
||
def auto_reload_hook(): | ||
print "Server is starting: " + str(datetime.datetime.now()) | ||
|
||
|
||
application = tornado.web.Application(urlpatterns) | ||
if __name__ == "__main__": | ||
application.listen(8888) | ||
tornado.ioloop.IOLoop.instance().start() | ||
tornado.autoreload.add_reload_hook(auto_reload_hook) | ||
tornado.autoreload.start() | ||
print "Server is starting: " + str(datetime.datetime.now()) | ||
try: | ||
tornado.ioloop.IOLoop.instance().start() | ||
except KeyboardInterrupt: | ||
print "" | ||
print "Quitting" | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
|
||
TEMPLATE_PATH = os.path.dirname(os.path.abspath(__file__)) + "/templates/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from views.web import * | ||
from views.socket import * | ||
|
||
|
||
urlpatterns = [ | ||
(r"/", FrontPage), | ||
(r"/socket", ClientSocket), | ||
(r"/push", Pusher), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__author__ = 'masnun' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from tornado.websocket import WebSocketHandler | ||
from core.socket_storage import SOCKETS | ||
|
||
class ClientSocket(WebSocketHandler): | ||
def open(self): | ||
SOCKETS.append(self) | ||
print "WebSocket opened" | ||
|
||
def on_close(self): | ||
print "WebSocket closed" | ||
SOCKETS.remove(self) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from core.handlers import WebRequestHandler | ||
from core.socket_storage import SOCKETS | ||
|
||
|
||
class FrontPage(WebRequestHandler): | ||
def get(self): | ||
self.render('chat.html', {'host': self.request.host}) | ||
|
||
|
||
class Pusher(WebRequestHandler): | ||
def get(self, *args, **kwargs): | ||
data = self.get_argument('data') | ||
for socket in SOCKETS: | ||
socket.write_message(data) | ||
self.write('Posted') |