0

Ok, I've read a million tutorials and in the comments section there is always someone who is asking the same thing as what I am now experiencing and no real answer is given, And just to clarify, yes, im using latest version of chrome which websockets works on.

I keep getting the "Disconnected" error as soon as the page loads.

firstly im runnig PHP 5 on IIS (not my choice)

I have downloaded http://bohuco.net/dev/websocket/?source=WebSocketServer.php

and trying to test out http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/.

The location of the files that I have downloaded are in www.something.co.uk/html5/test

which includes the demo files above and also the websocket php.

The thing that I am confused about is how //Connect to the server

var server = new ServerEventsDispatcher("<someip>:12345");

connects with the server.php located in file path above.

can I add the domain and URL in the someip part?

Any help on this is much appreciated.

EDIT

after some more reading it appears that I can.

http://dev.w3.org/html5/websockets/

var socket = new WebSocket('ws://game.example.com:12010/updates');
socket.onopen = function () {
  setInterval(function() {
    if (socket.bufferedAmount == 0)
      socket.send(getUpdateData());
  }, 50);
};

So my next question (as this still does not resolve the situation)

within the server.php file, is localhost valid or does it have to be ip/domain?

$server = new WebsocketServer('localhost', 12345, 'process');

EDIT

I have changed web socket connection to

//Connect to the server
var server = new ServerEventsDispatcher("domain.co.uk:81/path/to/server.php");

and changed the port in scripts.php to 81 and still nothing...

Phil Jackson
  • 10,238
  • 23
  • 96
  • 130

1 Answers1

2

The WebSocketServer class you have linked launches said Server on the port given while creating the object. Note that this port cannot be the same as your webserver due to obvious reasons.

The javascript side needs to open a WebSocket to that server, assuming that ServerEventsDispatcher internally opens a new WebSocket on the ws://:12345/ you need to add the host name or ip address of the server running the WebSocketServer with the correct port number there.

So, say the WebSocketServer runs on someserver.domain.com on port 81 (since 80 is most likely used by IIS):

var server = new ServerEventsDispatchet("someserver.domain.com:81");

Looking at the code for that javascript class it actually opens a WebSocket using the address directly; so you could also use ws://someserver.domain.com:81 there if you want to specify the protocol (or wss for SSL).

Edit in followup of your edit: I'd use the public address for your server in that WebSocketServer class; as it will try to bind on it:

socket_bind($this->master, $this->address, $this->port) or die("socket_bind() failed");

see also: http://nl3.php.net/manual/en/function.socket-bind.php

In addition to your latest edit:

you shouldn't add the path to your php script. When you open your PHP script, it should try to open up that WebSocket server; but the websocket server will just run at "ws://<servername>:<port>". So in the javascript you can use that as address, and then browse to the php script in your browser for the test program.

-- Edit for server use --

Since this would become too long/unreadable in a comment; according to the site you have to use a php commandline to actually launch the server class:

"Open a terminal window (Command line on windows) and navigate to your project directory. Execute the script with PHP and if all went to plan you should get the following message:"

$ php -f server.php
Server Started : 2010-05-26 22:40:03
Listening on   : localhost port 12345
Master socket  : Resource id #5

When that's done; the server should be up and listening. You can then connect to that server using the WebSocket in javascript with the websocket url as said above :)

Yhn
  • 2,785
  • 2
  • 13
  • 10
  • 1
    You could try checking if the websocketserver is actually running on the server by checking the netstat output. When you browse to the php script that launches the WebSocketServer, it should be shown there to be listening on the given port (:81). Also, your server must allow traffic incoming on port 81. – Yhn Sep 09 '11 at 10:17
  • ! browsing to you url to server.php on port 81 doesn't work but does on port 80! let me investigate more! – Phil Jackson Sep 09 '11 at 10:21
  • I think the main issue is that you have a wrong view of what the script does. The server.php class actually launches a second(!) server on port 81 that accepts websocket traffic. The websocket itself is not handled through IIS, but the PHP implemented server. Hence why the WebSocket in javascript should just connect to `ws://server:port/` without the /path/to/server.php – Yhn Sep 09 '11 at 10:23
  • And how do I activate the script.php then? does it just run all the time? – Phil Jackson Sep 09 '11 at 10:28
  • I read through the article a bit and added the relevant part in my answer above :) – Yhn Sep 09 '11 at 10:36
  • would this mean it would always run? also how come you have to run it from command line and cant just run it from the browser? also, is there a function to call process command line commands as I dont have that much access? - I do appreciate all this. – Phil Jackson Sep 09 '11 at 10:40
  • The reason why it has to be commandline is because otherwise the server would stop running on termination of the php script. You could try using php's exec()[1] function; but if you don't have that much access it might get hard to actually stop or restart the server. [1] http://php.net/manual/en/function.exec.php – Yhn Sep 09 '11 at 10:43
  • So in theory, if I opened up a browser, ran the server.php, opened another browser (supporting websockets) and tested it should work, and after I closed it it would disconnect? – Phil Jackson Sep 09 '11 at 10:46
  • You might actually pull it off like that; but keep in mind that you'd might need to set the script timeout in the server.php to make sure php doesn't terminate the running script after 30 seconds. See: http://nl3.php.net/manual/en/function.set-time-limit.php – Yhn Sep 09 '11 at 10:48
  • I've got gotta say a big thank you. I'm going to go away and test this. Going to accept this as the answer. regards – Phil Jackson Sep 09 '11 at 10:53
  • Can you get the server.php script run locally on the commandline? Cuase if you get that to run; opening the html+javascript client page should be able to connect while using ws://localhost:81/ as url. Otherwise; what doesn't work? – Yhn Sep 09 '11 at 13:54
  • Okay... after some trying myself it seems like the phpwebsocket (or the one you linked) simply doesn't seem to work with the chrome nightly anymore... so I'm guessing it's a dead end until a newer server version is found. I used the Jetty-websockets in java to create a websocket server; that does work with the chrome nightly... but I'm afraid that doesn't help you much :( – Yhn Sep 09 '11 at 14:36