Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have setup an application using sockets. The way that I do it is that my main App file has a socket object and then receives messages from socket.io and puts them into an array. Any controllers that might be interested in listening for certain socket messages bind to the array and get new messages that come in. For example my chatController cares about chat messages, and my eventController cares about showing or hiding images based on socket events that are fired. </p> <p>Below is some coffeescript code of how I set this up.</p> <pre><code>window.App = Ember.Application.create init: -&gt; @_super() @setSocketIO() ## # socket - socket.io object for communicating with the server socket: null ## # socketMessages - array used to store any socket.io messages emitted from the server socketMessages: [] ## # setSocketIO - setup socket.io connection and message endpoints setSocketIO: -&gt; @set 'socket', io.connect() ## # if the socket errors out, then reconnect it App.socket.on "error", (err) -&gt; App.socket.socket.reconnect() ## # receive new messages App.socket.on "event-message-receive", (data) =&gt; @createSocketMessage 'event-message-receive', data ## # image url recieved App.socket.on "event-image-set-receive", (data) =&gt; @createSocketMessage 'event-image-set-receive', data ## # hide image recieved App.socket.on "event-image-hide-receive", () =&gt; @createSocketMessage 'event-image-hide-receive' </code></pre> <p>and then in my chat controller I only listen for new messages received</p> <pre><code>App.ChatController = Ember.ArrayController.extend ## # chat messages chatMessages: [] ## # socketMessageBinding - bind to the App.socketMessage message queue for receiving new messages from the server socketMessagesBinding: 'App.socketMessages' ## # socketMessageAdded - called whenever a socket.io message is sent from teh server socketMessageAdded: (-&gt; # get the newest item on the stack socketMessage = @socketMessages[@socketMessages.length-1] if socketMessage.type == 'event-message-receive' @chatMessages.pushObject socketMessage.data ).observes('socketMessages.@each') </code></pre> <p>and in my event controller I listen for image show and image hide</p> <pre><code>App.EventController = Ember.ArrayController.extend ## # property to show or hide an img on the page showImage: false ## # socketMessageBinding - bind to the App.socketMessage message queue for receiving new messages from the server socketMessagesBinding: 'App.socketMessages' ## # socketMessageAdded - called whenever a socket.io message is sent from teh server socketMessageAdded: (-&gt; # get the newest item on the stack socketMessage = @socketMessages[@socketMessages.length-1] if socketMessage.type == 'event-image-set-receive' @set 'showImage', true else if socketMessage.type == 'event-image-hide-receive' @set 'showImage', false ).observes('socketMessages.@each') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload