HTML 5 Web 套接字 [英] HTML 5 Web Sockets

查看:35
本文介绍了HTML 5 Web 套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经阅读了一百万篇教程,并且在评论部分总是有人问与我现在正在经历的相同的事情,但没有给出真正的答案,只是为了澄清,是的,我使用的是最新的websockets 支持的 chrome 版本.

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.

首先我在 IIS 上运行 PHP 5(不是我的选择)

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

我已经下载了 http://bohuco.net/dev/websocket/?source=WebSocketServer.php

并尝试测试 http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/.

我下载的文件位置在www.something.co.uk/html5/test

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

其中包括上面的演示文件以及 websocket php.

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");

连接位于上面文件路径中的 server.php.

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

可以在someip部分添加域名和网址吗?

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

非常感谢您对此的任何帮助.

Any help on this is much appreciated.

编辑

经过更多阅读后,我似乎可以.

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)

在 server.php 文件中,localhost 是有效的还是必须是 ip/domain?

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

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

编辑

我已将 Web 套接字连接更改为

I have changed web socket connection to

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

并将scripts.php中的端口更改为81,但仍然没有...

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

推荐答案

您链接的 WebSocketServer 类在创建对象时在给定的端口上启动所述服务器.请注意,由于显而易见的原因,此端口不能与您的网络服务器相同.

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.

javascript端需要向那个服务器打开一个WebSocket,假设ServerEventsDispatcher内部在ws://:12345/上打开了一个新的WebSocket,你需要加上运行WebSocketServer的服务器的主机名或者ip地址正确的端口号.

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.

所以,假设 WebSocketServer 在 someserver.domain.com 上的 81 端口上运行(因为 80 很可能被 IIS 使用):

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");

查看那个javascript类的代码,它实际上直接使用地址打开了一个WebSocket;所以如果你想指定协议(或 SSL 的 wss),你也可以在那里使用 ws://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).

在您的编辑后进行我会在那个 WebSocketServer 类中使用你的服务器的公共地址;因为它会尝试绑定它:

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");

另见:http://nl3.php.net/manual/en/function.socket-bind.php

除了您最近的

您不应该将路径添加到您的 php 脚本中.当你打开你的 PHP 脚本时,它应该尝试打开那个 WebSocket 服务器;但是 websocket 服务器只会在 "ws://:" 上运行.所以在javascript中你可以使用它作为地址,然后在你的浏览器中浏览到测试程序的php脚本.

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 --

因为这会在评论中变得太长/不可读;根据网站,您必须使用 php 命令行来实际启动服务器类:

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:

打开终端窗口(Windows 上的命令行)并导航到您的项目目录.使用 PHP 执行脚本,如果一切按计划进行,您应该收到以下消息:"

"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

完成后;服务器应该启动并监听.然后,您可以使用 javascript 中的 WebSocket 和 websocket url 连接到该服务器,如上所述:)

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 :)

这篇关于HTML 5 Web 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆