Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Making it accept pageUrl differences and handle them

  • Loading branch information...
commit c2328b6c2c2022e8982b8bd4026f664c1eb04804 1 parent ff330be
Paul Bellamy paulbellamy authored

Showing 2 changed files with 26 additions and 9 deletions. Show diff stats Hide diff stats

  1. +21 7 server.go
  2. +5 2 static/js/application.js
28 server.go
@@ -13,19 +13,33 @@ var client redis.Client
13 13 var sio *socketio.SocketIO
14 14
15 15 func socketIOConnectHandler(c *socketio.Conn) {
16   - j, _ := json.Marshal(PaginateFor("/", 0, 10))
17   - c.Send("{\"event\":\"initial\", \"data\":" + string(j) + "}")
18 16 }
19 17
20 18 func socketIODisconnectHandler(c *socketio.Conn) {
21 19 }
22 20
23 21 func socketIOMessageHandler(c *socketio.Conn, msg socketio.Message) {
24   - if comment, err := Create([]uint8(msg.Data())); err == nil {
25   - log.Println("Stored Comment: ", comment.ToJson())
26   - sio.Broadcast("{\"event\":\"comment\", \"data\":" + comment.ToJson() + "}")
27   - } else {
28   - log.Println("Error Storing Comment: ", err)
  22 + var j map[string]string
  23 + err := json.Unmarshal([]uint8(msg.Data()), &j)
  24 +
  25 + if err != nil {
  26 + log.Println("Error Parsing Message: ", err)
  27 + return
  28 + }
  29 +
  30 + switch j["event"] {
  31 + case "initial":
  32 + log.Println("got initial: ", j["data"])
  33 + r, _ := json.Marshal(PaginateFor(j["data"], 0, 10))
  34 + c.Send("{\"event\":\"initial\", \"data\":" + string(r) + "}")
  35 + case "comment":
  36 + log.Println("got comment: ", j["data"])
  37 + if comment, err := Create([]uint8(j["data"])); err == nil {
  38 + log.Println("Stored Comment: ", comment.ToJson())
  39 + sio.Broadcast("{\"event\":\"comment\", \"data\":" + comment.ToJson() + "}")
  40 + } else {
  41 + log.Println("Error Storing Comment: ", err)
  42 + }
29 43 }
30 44 }
31 45
7 static/js/application.js
@@ -99,8 +99,8 @@ var LiveCommentsView = Backbone.View.extend({
99 99 if (body.val() != '') {
100 100 var newComment= new models.Comment({ Author: author.val()
101 101 , Body: body.val()
102   - , PageUrl: '/'});
103   - this.socket.send(newComment.toJSON());
  102 + , PageUrl: window.location.origin + window.location.pathname});
  103 + this.socket.send(JSON.stringify({event: "comment", data: JSON.stringify(newComment.toJSON())}));
104 104 body.val('');
105 105 }
106 106 }
@@ -117,6 +117,9 @@ var LiveCommentsController = {
117 117
118 118 this.socket.on('message', function(msg) {view.msgReceived(msg)});
119 119 this.socket.connect();
  120 + this.socket.send(JSON.stringify({ event: "initial"
  121 + , data: window.location.origin + window.location.pathname
  122 + }));
120 123
121 124
122 125 return this;

0 comments on commit c2328b6

Please sign in to comment.
Something went wrong with that request. Please try again.