2013-05-10 53 views
0

我设计了一个基于websocket协议的聊天应用程序。从远程计算机访问websocket协议

我已经在计算机A上安装了xampp服务器。我在计算机A中启动服务器,然后尝试从计算机B使用ip地址访问client.php页面(url是ipadresssofA/project/client.php)的电脑A.但它显示套接字错误。

而当我在电脑A上尝试相同的网址(url是ipadresssofA/project/client.php)。客户端连接。两台计算机都与相同的WIFI连接。 这里是我的客户code.please告诉我什么是错

<html> 
<head> 


<style> 

#chatlog {width:440px; height:200px; border:1px solid;overflow:auto;} 
    #userslog {width:440px; height:200px; border:1px solid;overflow:auto;} 
#msg {width:330px; height:100px;} 
</style> 

<script> 



function initialize(){ 
    var host = "ws://localhost:12345/project/server3z.php"; 
    try{ 
    socket = new WebSocket(host); 
    chatlog('WebSocket - status '+socket.readyState); 
    socket.onopen = function(event){chatlog("WebSocket status "+this.readyState); }; 
    socket.onmessage = function(event){ chatlog(event.data); }; 
    socket.onclose = function(){ chatlog("WebSocket status "+this.readyState); }; 
socket.onerror = function(event){chatlog("Error :"+event.data); }; 
    } 
    catch(err){ chatlog(err); } 

} 

function send() 
{ 
    var chat; 

    chat= document.getElementById("msg").value; 
    if(!chat){ alert("Message can not be empty"); return; } 

    try{ socket.send(chat); chatlog('Sent: '+chat); } catch(err){ log(err); } 
    document.getElementById("msg").value = ""; 
} 
function quit(){ 
    chatlog("closed!"); 
    socket.close(); 
    chatlog("WebSocket status "+socket.readyState); 

} 



function chatlog(msg) 
{ 
var match=msg.match(/10101010101010/g); 
if(match) 
{ 
var msg=msg.split("10101010101010"); 
document.getElementById("userslog").innerHTML+="<br>"+msg[0]; 
} 
else 
{ 
document.getElementById("chatlog").innerHTML+="<br>"+msg; 
} 
} 

function onkey(event){ if(event.keyCode==13){ send(); } } 

</script> 

</head> 
<body onload="initialize()"> 
<center> 
<div id="chatlog"></div> 
<input id="msg" type="textbox" onkeypress="onkey(event)"/> 
<button onclick="send()">Send</button> 
<button onclick="quit()">Stop</button> 
<div id="userslog"></div> 
</center> 
</body> 
</html> 

而且在我的服务器代码的东西是写在评论,我想告诉你:

// start the server 
$Server = new PHPWebSocket(); 
$Server->bind('message', 'wsOnMessage'); 
$Server->bind('open', 'wsOnOpen'); 
$Server->bind('close', 'wsOnClose'); 
// for other computers to connect, you will probably need to change this to your LAN IP or external IP, 
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME'])) 
$Server->wsStartServer('localhost', 12345); 

回答

0

初始化VAR主机如下:

var host =“ws:// localhost:12345”;

其他一切看起来都不错。