Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Making it accept pageUrl differences and handle them

  • Loading branch information...
commit c2328b6c2c2022e8982b8bd4026f664c1eb04804 1 parent ff330be
@paulbellamy paulbellamy authored
Showing with 26 additions and 9 deletions.
  1. +21 −7 server.go
  2. +5 −2 static/js/application.js
View
28 server.go
@@ -13,19 +13,33 @@ var client redis.Client
var sio *socketio.SocketIO
func socketIOConnectHandler(c *socketio.Conn) {
- j, _ := json.Marshal(PaginateFor("/", 0, 10))
- c.Send("{\"event\":\"initial\", \"data\":" + string(j) + "}")
}
func socketIODisconnectHandler(c *socketio.Conn) {
}
func socketIOMessageHandler(c *socketio.Conn, msg socketio.Message) {
- if comment, err := Create([]uint8(msg.Data())); err == nil {
- log.Println("Stored Comment: ", comment.ToJson())
- sio.Broadcast("{\"event\":\"comment\", \"data\":" + comment.ToJson() + "}")
- } else {
- log.Println("Error Storing Comment: ", err)
+ var j map[string]string
+ err := json.Unmarshal([]uint8(msg.Data()), &j)
+
+ if err != nil {
+ log.Println("Error Parsing Message: ", err)
+ return
+ }
+
+ switch j["event"] {
+ case "initial":
+ log.Println("got initial: ", j["data"])
+ r, _ := json.Marshal(PaginateFor(j["data"], 0, 10))
+ c.Send("{\"event\":\"initial\", \"data\":" + string(r) + "}")
+ case "comment":
+ log.Println("got comment: ", j["data"])
+ if comment, err := Create([]uint8(j["data"])); err == nil {
+ log.Println("Stored Comment: ", comment.ToJson())
+ sio.Broadcast("{\"event\":\"comment\", \"data\":" + comment.ToJson() + "}")
+ } else {
+ log.Println("Error Storing Comment: ", err)
+ }
}
}
View
7 static/js/application.js
@@ -99,8 +99,8 @@ var LiveCommentsView = Backbone.View.extend({
if (body.val() != '') {
var newComment= new models.Comment({ Author: author.val()
, Body: body.val()
- , PageUrl: '/'});
- this.socket.send(newComment.toJSON());
+ , PageUrl: window.location.origin + window.location.pathname});
+ this.socket.send(JSON.stringify({event: "comment", data: JSON.stringify(newComment.toJSON())}));
body.val('');
}
}
@@ -117,6 +117,9 @@ var LiveCommentsController = {
this.socket.on('message', function(msg) {view.msgReceived(msg)});
this.socket.connect();
+ this.socket.send(JSON.stringify({ event: "initial"
+ , data: window.location.origin + window.location.pathname
+ }));
return this;
Please sign in to comment.
Something went wrong with that request. Please try again.