Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the Pyramid-specific files to the project
Added credits to README.txt for the inspiration of how to create the chat window.
- Loading branch information
Kirk Strauser
committed
May 20, 2011
1 parent
db6d69e
commit 8f7b4b1
Showing
6 changed files
with
160 additions
and
0 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
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,10 @@ | ||
#!/usr/bin/env python | ||
|
||
def main(global_config, **settings): | ||
|
||
# ... | ||
|
||
# These routes handle viewing the chat window and sending/receiving messages | ||
config.add_route('chat', '/chat', view='myapp.views.chat.chat', renderer='myapp:templates/chatwindow.pt') | ||
config.add_route('chatrecv', '/chat/recvmessage/{chatid}', view='myapp.views.chat.recvmessage', renderer='json') | ||
config.add_route('chatsend', '/chat/sendmessage/{chatid}', view='myapp.views.chat.sendmessage', renderer='json') |
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,7 @@ | ||
[app:MyApp] | ||
# All the normal application settings... | ||
reload_templates = true | ||
debug_authorization = false | ||
debug_notfound = false | ||
# ...plus: | ||
seshat_sqlitedb = /tmp/seshat.db |
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,34 @@ | ||
/* CSS Document */ | ||
body { | ||
font:12px arial; | ||
color: #222; | ||
text-align:center; | ||
padding:35px; } | ||
|
||
#wrapper { | ||
margin:0 auto; | ||
padding-bottom:25px; | ||
padding-top:25px; | ||
background:#EBF4FB; | ||
width:504px; | ||
border:1px solid #ACD8F0; } | ||
|
||
#chatbox { | ||
text-align:left; | ||
margin:0 auto; | ||
margin-bottom:25px; | ||
padding:10px; | ||
background:#fff; | ||
height:270px; | ||
width:430px; | ||
border:1px solid #ACD8F0; | ||
overflow:auto; } | ||
|
||
#sendtext { | ||
width:395px; | ||
border:1px solid #ACD8F0; } | ||
|
||
#sendbutton { width: 60px; } | ||
|
||
span.tovisitor { color: red; } | ||
span.fromvisitor { color: blue; } |
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,69 @@ | ||
<!DOCTYPE html PUBLIC "- //W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<title>Chat - Customer Module</title> | ||
<link type="text/css" rel="stylesheet" href="/static/chatstyle.css" /> | ||
</head> | ||
<body> | ||
<div id="wrapper"> | ||
<div id="menu"> | ||
</div> | ||
|
||
<div id="chatbox">Messages you send will <span class="fromvisitor">look like this</span>, and messages you receive will <span class="tovisitor">look like this</span>.</div> | ||
|
||
<form name="message" action=""> | ||
<input name="sendtext" type="text" id="sendtext" size="63" /> | ||
<input name="action" type="submit" id="sendbutton" value="Send" /> | ||
</form> | ||
</div> | ||
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> | ||
<script type="text/javascript"> | ||
// jQuery Document | ||
$(document).ready(function(){ | ||
|
||
// Append the new message to the bottom of the chat window, and scroll | ||
// the window to display it if needed | ||
function updateChat(direction, message){ | ||
if(message == '') { return false; } | ||
// Let the browser's DOM escape any HTML entities contained in the message | ||
var encoder = document.createElement("div"); | ||
encoder.innerText = encoder.textContent = message; | ||
message = encoder.innerHTML; | ||
delete encoder; | ||
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request | ||
$("#chatbox").append('<br /><span class="' + direction + 'visitor">' + message + '</span>'); | ||
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request | ||
if(newscrollHeight > oldscrollHeight){ | ||
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div | ||
} | ||
} | ||
|
||
// User presses "enter" or clicks the "Send" button | ||
$("#sendbutton").click(function(){ | ||
var sendtext = $("#sendtext").val(); | ||
// Don't pester the server with empty messages | ||
if(sendtext == '') { return false; } | ||
$.post("/chat/sendmessage/${chatid}", {text: sendtext}); | ||
$("#sendtext").attr("value", ""); | ||
updateChat('from', sendtext); | ||
return false; | ||
}); | ||
|
||
// Poll the server for new messages to display | ||
function getNewMessage(){ | ||
$.ajax({ | ||
url: "/chat/recvmessage/${chatid}", | ||
cache: false, | ||
success: function(response){ | ||
updateChat('to', response); | ||
}, | ||
}); | ||
}; | ||
|
||
setInterval (getNewMessage, 1000); | ||
|
||
$("#sendtext").focus(); | ||
}); | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Allow web visitors to chat with local users logged into a Jabber server""" | ||
|
||
from pyramid.security import authenticated_userid | ||
from seshat import client | ||
|
||
def getseshatvalues(request): | ||
"""Return a configured Seshat client and the username of the visitor""" | ||
user = authenticated_userid(request) | ||
return (client.SeshatClient(request.registry.settings['seshat_sqlitedb']), | ||
user if user is not None else 'Anonymous') | ||
|
||
def chat(request): | ||
"""Get a new chatid and open the chat window""" | ||
message = 'Coming from page %s' % request.referer if request.referer is not None else None | ||
seshatclient, user = getseshatvalues(request) | ||
return {'chatid': seshatclient.startchat(user, message)} | ||
|
||
def recvmessage(request): | ||
"""Poll the server for the first queued message in this chat""" | ||
chatid = int(request.matchdict['chatid']) | ||
seshatclient, user = getseshatvalues(request) | ||
message = seshatclient.getmessage(chatid, user) | ||
if message is None: | ||
return '' | ||
return message | ||
|
||
def sendmessage(request): | ||
"""Queue the visitor's message for delivery to the chat's localuser""" | ||
chatid = int(request.matchdict['chatid']) | ||
seshatclient, user = getseshatvalues(request) | ||
seshatclient.sendmessage(chatid, user, request.params['text']) |