0

I accessed my client page using this link

http://XX.XX.XX.XX/project/client.php 

.It has few lines of javascript and html but its not working at all . But when i access my client page using this link

http://localhost/project/client.php 

, it works. I know something i should change in javascript code but i dont know what . So please tell me . Here is my client code :

<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>
1
  • 1
    What you mean by not working??? Are you getting any errors? Commented May 9, 2013 at 16:11

5 Answers 5

6

Don't hard-code the host to localhost, use location.hostname instead:

var host = "ws://" + location.hostname + ":12345/project/server3z.php";
Sign up to request clarification or add additional context in comments.

6 Comments

hey , what is location.hostname ? can you give example please ?
@AnubhavJhalani location.hostname is the part of the current URL after the scheme, and before the first slash, excluding the port. Example: www.google.com, stackoverflow.com. In your case, when you access the page through http://localhost:80/, location.hostname will be "localhost" - no problem. However, when you visit the page through http://192.168.1.0/, your current method will fail (because you've hardcoded http://localhost:12345). Your code will start to work again if you use location.hostname.
so u want to say that i should use var host = "ws://192.168.1.0:12345/project/server3z.php" ?? am i right ?
@AnubhavJhalani No, not at all. I say that you should not put the literal local URL in the string, but use the location.hostname property, as shown in my answer. If you use host = "ws://192.168.1.0:12345/, you'll see that your code breaks when you move to a different network.
ohk i got it . thank you very much . i will check it and will back to you later :)
|
1

You are using WebSocket are you sure that your browser in the other PC it's supports HTML5 and WebsoCKET?

WebSocket was introduced early

3 Comments

I mean why downvote? The title said: "accessing from remote computer"... so the browser on that remote pc might not support WebSocket.
Did you solve? I'm not asking you for a vote up. I'm just disappointed for the vote down. So.. at least let me know if was for this reason :-)
yes you guessed right. i was using that browswer which does not support websocket
0

this line i sthe problem var host = "ws://localhost:12345/project/server3z.php"; localhost by default means your local machine. so when you access it from you local machine it maps to the correct machine but when you access it from a remote server it just searches that server because now the localhost is changed

Comments

0

In your code you have a hardcoded reference to a url on your local host:

var host = "ws://localhost:12345/project/server3z.php";

If you want to access it from another computer, you'll need to replace that with a domain or ip address that the remote client can resolve.

Comments

0

thanks for answer !! :)

madthew was right . I was using IEwhich does not support websocket. It is working in my mozila now.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.