Last active
August 29, 2015 14:06
-
-
Save ILiedAboutCake/656b04bd79109b843209 to your computer and use it in GitHub Desktop.
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
import threading | |
import tornado.httpserver | |
import tornado.websocket | |
import tornado.ioloop | |
import tornado.web | |
import json | |
import socket | |
#dem variables | |
numClients = 0 | |
strims = {} | |
#takes care of updating console | |
def printStatus(): | |
threading.Timer(5.0, printStatus).start() | |
print 'Currently connected clients: ' + str(numClients) | |
for key, value in strims.items(): | |
print key, value | |
#Stat tracking websocket server | |
class WSHandler(tornado.websocket.WebSocketHandler): | |
def check_origin(self, origin): | |
return True | |
def open(self): | |
global numClients | |
print 'New connection: (' + self.request.remote_ip + ') ' + socket.getfqdn(self.request.remote_ip) | |
numClients += 1 | |
data_string = json.dumps(strims) | |
self.write_message(json.dumps(strims)) | |
def on_message(self, message): | |
global strims | |
fromClient = json.loads(message) | |
#handle session counting | |
if fromClient[u'action'] == "join": | |
strims.setdefault(fromClient[u'strim'], 0) | |
strims[fromClient[u'strim']] += 1 | |
print 'User Connected: Watching %s' % (fromClient[u'strim']) | |
else: | |
strims.setdefault(fromClient[u'strim'], 0) | |
strims[fromClient[u'strim']] -= 1 | |
print 'User Disconnected: Was Watching %s' % (fromClient[u'strim']) | |
#remove the dict key if nobody is watching DaFeels | |
if strims[fromClient[u'strim']] == 0: | |
strims.pop(fromClient[u'strim'], None) | |
def on_close(self): | |
global numClients | |
print self.close_reason | |
numClients -= 1 | |
#print console updates | |
printStatus() | |
#JSON api server | |
class APIHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write(json.dumps({"streams":strims, "totalviewers":numClients})) | |
application = tornado.web.Application([ | |
(r'/ws', WSHandler), | |
(r'/api', APIHandler) | |
]) | |
if __name__ == "__main__": | |
http_server = tornado.httpserver.HTTPServer(application) | |
http_server.listen(9998) | |
tornado.ioloop.IOLoop.instance().start() |
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
<!doctype html> | |
<html> | |
<head> | |
<title>WebSockets Hello World</title> | |
<meta charset="utf-8" /> | |
<style type="text/css"> | |
body { | |
text-align: center; | |
min-width: 500px; | |
} | |
</style> | |
<script src="http://code.jquery.com/jquery.min.js"></script> | |
<script> | |
var ws = new WebSocket("ws://localhost:9998/ws"); | |
var sendObj = new Object(); | |
sendObj.strim = "/destinychat?stream=chanman"; | |
ws.onopen = function() { | |
sendObj.action = "join"; | |
ws.send(JSON.stringify(sendObj)); | |
}; | |
ws.onmessage = function (evt) { | |
console.log(evt.data); | |
}; | |
ws.onclose = function(evt) { | |
console.log(evt.data); | |
}; | |
$( window ).unload(function() { | |
sendObj.action = "unjoin"; | |
ws.send(JSON.stringify(sendObj)); | |
}); | |
</script> | |
</head> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment