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 r开发者_JAVA技巧eading 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...
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 :)
精彩评论