I am starting with this websocket and I am trying this shaft for my first project ..
I have installed the tutorial by executing this command in a command prompt composer.json Musician needs cboden / ratchet and Musician needs cboden / ratchet composer.lock
Rate I made chat.php which went copied code from quick example on file and ratchet Git:
& lt; Use php as Ratchet \ MessageComponentInterface; Use the shaft / connective interface; // Be sure that the composer dependency has been established __DIR__ '/vendor/autoload.php'; / ** * chat.php * Send to all the clients sent to all connected clients (excluding sender) * / Class MyChat applies MessageComponentInterface {secure $ clients; Public function __ composition () {$ this- & gt; Client = new \ SplObjectStorage; } Public Function Open (Connection Interface $ Conn) {$ this- & gt; Client- & gt; Attach ($ conn); } Public function on message (connection interface $, $ msg) {foreach ($ this-> as customer $ customer) {if ($ $! $$ client) {$ client- & gt; Send ($ msg); }}} Close at Public Function (Connection Interface $ conn) {$ this- & gt; Clients- & gt; Different ($ conn); } Error on Public Function (Connection Interface $ conn, \ exception $ e) {$ conn- & gt; Close (); }} // Port 8080 $ app = Run the server application through the WebSocket Protocol on the new shaft app ('localhost', 8080); $ App- & gt; Route ('/ chat', new macaat); $ App- & gt; Route ('/ echo', new shaft \ server \ eicosver, array ('*')); $ App- & gt; Run (); Then I execute this command at the command prompt: php chat.php
My client side still getting an error :
Chrome Unkit invalid StateError: Failed to execute 'send' to 'websacet': still in case of connecting.
Firefox Invalid StateError: An attempt has been made to use an object, which is no longer usable,
My Folding (at XAMPP):
Customer
htdocs / public / chat / index.php with a common. Js intact in which
var conn = new web site ('ws: // localhost: 8080 / echo'); Conn.onmessage = function (e) {console.log (e.data); }; Conn.send ('hello me!'); Server
htdocs / public / chatserver / chat.php htdocs / public / chatserver / vendor /
); Conn.onmessage = function (e) {console.log (e.data); }; Conn.onopen = function (e) {console.log ("connection established!"); Conn.send ('hello me!'); };
You should send you when you open the connection. It appears that you try it before the connection is established.
No comments:
Post a Comment