无法与位于 ws://localhost:8000/socket/server/startDaemon.php 的服务器建立连接。var socket = new WebSocket(host);
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6172364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host);
提问by Sanjeev Kumar Jha
I am using javascript to connect websocket:
我正在使用 javascript 连接 websocket:
<script>
var socket;
var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
</script>
I got the error:
我得到了错误:
Can't establish a connection to the server at
无法与服务器建立连接
var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
How can I solve this issue?
我该如何解决这个问题?
NOTE : I enabled websocket in mozilla to support web socket application. and when i run in chrome i got error:
注意:我在 mozilla 中启用了 websocket 以支持 web socket应用程序。当我在 chrome 中运行时,出现错误:
can't establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host);
采纳答案by Sanjeev Kumar Jha
I solved my error by following code through this link
我通过此链接遵循代码解决了我的错误
http://www.flynsarmy.com/2010/05/php-web-socket-chat-application/and created socketWebSocketTrigger.class.php file for response message where code as
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;
}
}
and added code in send function of 'WebSocketServer.php' for calling 'responseMessage' function which response request message
并在'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));
}
it's working great.
它工作得很好。
回答by Ibu
Apparently firefox 4 has websockets disabled because of vulnerabilities. To quote From this article:
显然 firefox 4 由于漏洞而禁用了 websocket。引自这篇文章:
WebSocket disabled in Firefox 4
Recent discoveries found that the protocol that Websocket works with is vulnerable to attacks. Adam Barth demonstrated some serious attacks against the protocol that could be used by an attacker to poison caches that sit in between the browser and the Internet.
最近的发现发现 Websocket 使用的协议容易受到攻击。Adam Barth 展示了对该协议的一些严重攻击,攻击者可以使用这些攻击来毒化位于浏览器和 Internet 之间的缓存。
回答by King Skippus
Are you trying to run the client in Firefox? According to the documentation:
您是否尝试在 Firefox 中运行客户端?根据文档:
As of Feb/10 the only browsers that support websockets are Google Chrome and Webkit Nightlies. Get it from here http://www.google.com/chrome
截至 2 月 10 日,唯一支持 websockets 的浏览器是 Google Chrome 和 Webkit Nightlies。从这里获取它 http://www.google.com/chrome
Try running it in Chrome and see if that works for you.
尝试在 Chrome 中运行它,看看它是否适合你。
回答by Sujeet
First of all your mistake is using php function with javascript require_once 'WebSocket.php';
and secondly go through the tutorial as in the link below.
首先,您的错误是在 javascript 中使用了 php 函数,require_once 'WebSocket.php';
其次是通过下面的链接中的教程。
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
it's working fine.
它工作正常。
Thanks,
谢谢,