03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Real Time Chat 193<br />

27 });<br />

28<br />

29 App.IndexView = Ember.View.extend({<br />

30 "keyDown" : function(e) {<br />

31 this.get("controller").send("send", e.keyCode);<br />

32 }<br />

33 });<br />

.<br />

This was extracted from public/js/shared.js.<br />

We create IndexController and IndexView objects. The IndexView object intercepts the keyDown<br />

event and passes it to the IndexController object. The first bit of logic tells the send() method to<br />

ignore all keystrokes that aren’t the enter key. This means enter will trigger the send() method.<br />

It continues by checking for the presence of a /name command switch. If that’s present in the input<br />

value (via this.get(“command”)) then it sends a message to the server to change the user’s name.<br />

Otherwise it sends a normal message to the server. In order for the UI to update for the person<br />

sending the message; we need to also slightly modify the Chat class:<br />

1 public function onMessage(ConnectionInterface $socket, $message)<br />

2 {<br />

3 $user = $this->getUserBySocket($socket);<br />

4 $message = json_decode($message);<br />

5<br />

6 switch ($message->type)<br />

7 {<br />

8 case "name":<br />

9 {<br />

10 $user->setName($message->data);<br />

11 $this->emitter->emit("name", [<br />

12 $user,<br />

13 $message->data<br />

14 ]);<br />

15 break;<br />

16 }<br />

17<br />

18 case "message":<br />

19 {<br />

20 $this->emitter->emit("message", [<br />

21 $user,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!