问题描述
|
我正在使用javascript连接websocket:
<script>
var socket;
var host = \"ws://localhost:8000/socket/server/startDaemon.php\";
var socket = new WebSocket(host);
</script>
我得到了错误:
无法在以下位置建立与服务器的连接
var host = \"ws://localhost:8000/socket/server/startDaemon.php\";
var socket = new WebSocket(host);
我该如何解决这个问题?
注意:我在mozilla中启用了websocket以支持Web套接字应用程序。
当我在Chrome中运行时出现错误:
can\'t establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host);
解决方法
显然,由于漏洞,firefox 4禁用了websocket。引用本文的内容:
在Firefox 4中禁用了WebSocket
最近的发现发现Websocket使用的协议容易受到攻击。亚当·巴特(Adam Barth)展示了对该协议的严重攻击,攻击者可能会使用该协议来毒化位于浏览器和Internet之间的缓存。
, 我通过此链接通过遵循代码解决了我的错误
http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/
并创建用于响应消息的socketWebSocketTrigger.class.php文件,其中代码为
class socketWebSocketTrigger
{
function responseMessage($param)
{
$a = \'Unknown parameter\';
if($param == \'age\'){
$a = \"Oh dear,I\'m 152\";
}
if($param == \'hello\'){
$a = \'hello,how are you?\';
}
if($param == \'name\'){
$a = \'my name is Mr. websocket\';
}
if($param == \'today\'){
$a = date(\'Y-m-d\');
}
if($param == \'hi\'){
$a = \'hi there\';
}
return $a;
}
}
并在\'WebSocketServer.php \'的send函数中添加了代码,以调用\'responseMessage \'函数来响应请求消息
public function send($client,$msg){
$this->say(\"> \".$msg);
$messageRequest = json_decode($msg,true);
// $action=$messageRequest[0];
$action = \'responseMessage\';
$param = $messageRequest[1][\'data\'];
if( method_exists(\'socketWebSocketTrigger\',$action) ){
$response = socketWebSocketTrigger::$action($param);
}
$msg = json_encode(
array(
\'message\',array(\'data\' => $response)
)
);
$msg = $this->wrap($msg);
socket_write($client,$msg,strlen($msg));
}
效果很好。
, 您是否要在Firefox中运行客户端?根据文档:
截至10月2日,仅有的浏览器
支持网络套接字的是谷歌浏览器
和Webkit Nightlies。从这里获取
http://www.google.com/chrome
尝试在Chrome中运行它,看看是否适合您。
, 首先,您的错误是将php函数与javascriptrequire_once \'WebSocket.php\';
结合使用,其次,如下面的链接中所示,通过了本教程。
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
工作正常。
谢谢,