Last active
August 29, 2015 14:21
-
-
Save piotrmaslanka/dc6b9020063a80f1100b to your computer and use it in GitHub Desktop.
Paula Chat - Simplest presentation of Brilliant's sessions and REST server
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> | |
<head> | |
<title>Paula Internet Chat</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> | |
</head> | |
<body> | |
<div id="preinit"> | |
Nick: <input type="text" value="Brilliant Paula"><br> | |
<button>Zatwierdź</button> | |
</div> | |
<div id="chat" style="display: none;"> | |
<div id="messages"> | |
</div> | |
Wiadomość: <input type="text"><button>Wyślij</button> | |
</div> | |
<script type="text/javascript"> | |
var last_timestamp = 0; | |
var session_key = null; | |
if (location.host == '') | |
var target_url = 'http://localhost:81/'; | |
else | |
var target_url = 'http://'+location.host+':81/'; | |
function refresh_data() { | |
$.post(target_url, {order: 'get', from: last_timestamp, key: session_key}, function(data) { | |
for (var i=0; i<data.messages.length; i++) { | |
var msg = data.messages[i]; | |
last_timestamp = msg[0]; | |
$('#messages').append($('<div class="message"><strong>'+msg[1]+'</strong>: '+msg[2]+'</div>')); | |
} | |
}); | |
setTimeout(refresh_data, 3000); | |
} | |
function send_message() { | |
$.post(target_url, {order: 'send', key: session_key, message: $('#chat input').val()}); | |
} | |
$(function() { | |
$("#preinit button").click(function() { | |
var nick = $("#preinit input").val(); | |
$.post(target_url, {order: 'login', login: nick}, function(data) { | |
$("#preinit").hide(); | |
$("#chat").show(); | |
session_key = data.key; | |
refresh_data(); | |
}); | |
}); | |
$("#chat button").click(send_message); | |
}); | |
</script> | |
</body> | |
</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
from brilliant.app import setup | |
from brilliant.smep import start_smep | |
from brilliant.smep import SMEPHandler | |
from brilliant.objects import Component, Session, wire | |
import itertools, time | |
@Component('usersession', scope=Component.SCOPE_SESSION) | |
class UserSession(object): | |
def __init__(self, login): | |
self.login = login | |
def order(self, args): | |
if args['order'] == 'send': | |
wire('chatcontents').send_message(self.login, args['message']) | |
elif args['order'] == 'get': | |
from_time = float(args.get('from', 0)) | |
messages = wire('chatcontents').get_messages_since(from_time) | |
return {'messages': messages} | |
@Component('chatcontents') | |
class ChatContents(object): | |
def __init__(self): | |
self.messages = [] | |
def send_message(self, login, message): | |
self.messages.append((time.time(), login, message)) | |
def get_messages_since(self, from_time): | |
return list(itertools.ifilter(lambda x: x[0] > from_time, self.messages)) | |
class Handler(SMEPHandler): | |
def post(self, args): | |
if args['order'] == 'login': | |
login = args['login'] | |
sess = Session.create(expire=3600) # Expire in 1 hour | |
with sess: | |
UserSession(args['login']) | |
self.respond({'key': sess.key}) | |
else: | |
with Session.get(args['key']): | |
self.respond(wire('usersession').order(args)) | |
if __name__ == '__main__': | |
setup(session_auto_expire=True) | |
ChatContents() | |
start_smep(('0.0.0.0', 81), Handler) | |
while True: | |
time.sleep(100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment