Skip to content

Commit

Permalink
add abstract layer for websockets. issue #51
Browse files Browse the repository at this point in the history
  • Loading branch information
averrin committed Aug 19, 2013
1 parent cf66823 commit 6887ee5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
9 changes: 2 additions & 7 deletions activity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from django.views.generic import TemplateView, View
from braces.views import LoginRequiredMixin, JSONResponseMixin, AjaxResponseMixin
import misaka as m
import pusher
from activity.models import Event
from datetime import datetime
from emergent.websockets import Pusher


__all__ = (
Expand Down Expand Up @@ -44,18 +44,13 @@ def get_ajax(self, request, *args, **kwargs):

class ChatSendView(LoginRequiredMixin, JSONResponseMixin, AjaxResponseMixin, View):
def post_ajax(self, request, *args, **kwargs):
p = pusher.Pusher(
app_id=settings.PUSHER_APPID,
key=settings.PUSHER_KEY,
secret=settings.PUSHER_SECRET
)
msg = {
'message': m.html(self.request.POST['message']),
'user': self.request.user.username,
'type': 'chat',
}
Event(timestamp=datetime.now(), user=self.request.user, type='chat', message=msg['message']).save()
p['activity'].trigger('my_event', msg)
Pusher.send("activity", 'my_event', msg)
json_dict = {
"success": True
}
Expand Down
18 changes: 18 additions & 0 deletions emergent/websockets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pusher
from django.conf import settings


class PusherWrapper(object):

def __init__(self):

self.p = pusher.Pusher(
app_id=settings.PUSHER_APPID,
key=settings.PUSHER_KEY,
secret=settings.PUSHER_SECRET
)

def send(self, channel, event, msg):
self.p[channel].trigger(event, msg)

Pusher = PusherWrapper()
10 changes: 2 additions & 8 deletions static/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ $(function(){
"</div>"
);

var pusher = new Pusher('6bb5412badf09454aa87');
var channel = pusher.subscribe('activity');
channel.bind('my_event', function(data) {
console.log(data);
data.now = new Date().toLocaleTimeString();
$("#events").append(chat_message(data));
});

websocket_init()

var editor = new EpicEditor({
theme: {
base: "http://"+location.host+'/static/epiceditor/themes/base/epiceditor.css',
Expand Down
9 changes: 9 additions & 0 deletions static/js/vendor/mousetrap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions static/js/websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function websocket_init(){
var pusher = new Pusher('6bb5412badf09454aa87');
var channel = pusher.subscribe('activity');
channel.bind('my_event', function(data) {
console.log(data);
data.now = new Date().toLocaleTimeString();
$("#events").append(chat_message(data));
});

}
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<script type="text/javascript" src='{% static 'bootstrap/dist/js/bootstrap.js' %}'></script>
<script type="text/javascript" src='{% static 'notify.js' %}'></script>
<script type='text/javascript' src='http://js.pusher.com/2.1/pusher.min.js'></script>
<script type='text/javascript' src='{% static 'js/websockets.js' %}'></script>
<script type="text/javascript" src='{% static 'underscore/underscore.js' %}'></script>

<script type="text/javascript" src='{% static 'js/init.js' %}'></script>
Expand Down

0 comments on commit 6887ee5

Please sign in to comment.