03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Real Time Chat 181<br />

100 "message" => $message<br />

101 ]));<br />

102 }<br />

103 }<br />

104 }<br />

105<br />

106 public function onClose(ConnectionInterface $socket)<br />

107 {<br />

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

109<br />

110 if ($user)<br />

111 {<br />

112 $this->users->detach($user);<br />

113 $this->emitter->emit("close", [$user]);<br />

114 }<br />

115 }<br />

116<br />

117 public function onError(<br />

118 ConnectionInterface $socket,<br />

119 Exception $exception<br />

120 )<br />

121 {<br />

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

123<br />

124 if ($user)<br />

125 {<br />

126 $user->getSocket()->close();<br />

127 $this->emitter->emit("error", [$user, $exception]);<br />

128 }<br />

129 }<br />

130 }<br />

.<br />

This file should be saved as workbench/formativ/chat/src/Formativ/Chat/Chat.php.<br />

It’s fairly simple: the (delegate) onOpen and onClose methods handle creating new User objects and<br />

disposing of them. The onMessage method translates JSON-encoded message objects into required<br />

actions and responds back to the other socket connections with further details.

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

Saved successfully!

Ooh no, something went wrong!