-
Notifications
You must be signed in to change notification settings - Fork 1
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
15 changed files
with
263 additions
and
18 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,13 @@ | ||
from django.http import HttpResponse | ||
from django.views.generic.base import TemplateView | ||
from ws4redis.publisher import RedisPublisher | ||
|
||
|
||
class DemoChatView(TemplateView): | ||
template_name = 'index.html' | ||
|
||
def post(self, request, *args, **kwargs): | ||
redis_publisher = RedisPublisher( | ||
facility='demo', broadcast=True) | ||
redis_publisher.publish_message(request.POST.get('message')) | ||
return HttpResponse('OK') |
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
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
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,76 @@ | ||
@font-face { | ||
font-family: Roboto-light; | ||
src: url("../fonts/RobotoTTF/Roboto-Light.ttf"); | ||
} | ||
html, header, | ||
body, footer { | ||
margin: 0; | ||
padding: 0; | ||
width: 100%; | ||
font-family: Roboto-light, Helvetica, sans-serif; | ||
} | ||
hr { | ||
border: none; | ||
border-top: 1px solid rgba(150, 150, 150, 0.4); | ||
} | ||
textarea { | ||
margin: 0; | ||
width: 100%; | ||
border-radius: 5px; | ||
border-color: rgba(0, 0, 0, 0.2); | ||
font-family: Roboto-light, Helvetica, sans-serif; | ||
font-size: 16px; | ||
padding: 5px; | ||
} | ||
textarea:focus { | ||
border-color: rgba(0, 146, 219, 0.5); | ||
outline: none; | ||
} | ||
.list-inline { | ||
display: inline-block; | ||
} | ||
.text-center { | ||
text-align: center; | ||
} | ||
.container { | ||
width: 960px; | ||
margin: auto; | ||
padding: 20px 15px; | ||
} | ||
#messages { | ||
margin: 0; | ||
float: left; | ||
width: 45%; | ||
height: 300px; | ||
max-height: 300px; | ||
background-color: rgba(230, 230, 230, 0.5); | ||
overflow-y: scroll; | ||
padding: 15px; | ||
border-radius: 5px; | ||
} | ||
.message { | ||
font-size: 16px; | ||
line-height: 1.3em; | ||
} | ||
.form-wrapper { | ||
margin: 0; | ||
margin-left: 30px; | ||
float: left; | ||
width: 45%; | ||
} | ||
#send_message { | ||
border: 1px solid rgba(0, 0, 0, 0.3); | ||
border-radius: 5px; | ||
padding: 10px 15px; | ||
background-color: #fff; | ||
font-size: 18px; | ||
cursor: pointer; | ||
} | ||
#send_message:hover { | ||
transition: .3s; | ||
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5); | ||
} | ||
#send_message:focus { | ||
outline: none; | ||
border: 1px solid rgba(0, 146, 219, 0.8); | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,83 @@ | ||
|
||
function WS4Redis(options, $) { | ||
'use strict'; | ||
var opts, ws, deferred, timer, timer_interval = 0; | ||
var heartbeat_interval = null, missed_heartbeats = 0; | ||
|
||
if (this === undefined) | ||
return new WS4Redis(options, $); | ||
if (options.uri === undefined) | ||
throw new Error('No Websocket URI in options'); | ||
if ($ === undefined) | ||
$ = jQuery; | ||
opts = $.extend({ heartbeat_msg: null }, options); | ||
connect(opts.uri); | ||
|
||
function connect(uri) { | ||
try { | ||
console.log("Connecting to " + uri + " ..."); | ||
deferred = $.Deferred(); | ||
ws = new WebSocket(uri); | ||
ws.onopen = on_open; | ||
ws.onmessage = on_message; | ||
ws.onerror = on_error; | ||
ws.onclose = on_close; | ||
timer = null; | ||
} catch (err) { | ||
deferred.reject(new Error(err)); | ||
} | ||
} | ||
|
||
function send_heartbeat() { | ||
try { | ||
missed_heartbeats++; | ||
if (missed_heartbeats > 3) | ||
throw new Error("Too many missed heartbeats."); | ||
ws.send(opts.heartbeat_msg); | ||
} catch(e) { | ||
clearInterval(heartbeat_interval); | ||
heartbeat_interval = null; | ||
console.warn("Closing connection. Reason: " + e.message); | ||
ws.close(); | ||
} | ||
} | ||
|
||
function on_open() { | ||
console.log('Connected!'); | ||
timer_interval = 500; | ||
deferred.resolve(); | ||
if (opts.heartbeat_msg && heartbeat_interval === null) { | ||
missed_heartbeats = 0; | ||
heartbeat_interval = setInterval(send_heartbeat, 5000); | ||
} | ||
} | ||
|
||
function on_close(evt) { | ||
console.log("Connection closed!"); | ||
if (!timer) { | ||
// try to reconnect | ||
timer = setTimeout(function() { | ||
connect(ws.url); | ||
}, timer_interval); | ||
timer_interval = Math.min(timer_interval + 500, 5000); | ||
} | ||
} | ||
|
||
function on_error(evt) { | ||
console.error("Websocket connection is broken!"); | ||
deferred.reject(new Error(evt)); | ||
} | ||
|
||
function on_message(evt) { | ||
if (opts.heartbeat_msg && evt.data === opts.heartbeat_msg) { | ||
// reset the counter for missed heartbeats | ||
missed_heartbeats = 0; | ||
} else if (typeof opts.receive_message === 'function') { | ||
return opts.receive_message(evt.data); | ||
} | ||
} | ||
|
||
this.send_message = function(message) { | ||
ws.send(message); | ||
} | ||
} |
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