0

hi guys
i want to use php websockets... i made a web socket but it didnt work
i hope you guys know some javascript couz the client page is made with javascript and html5

this is the server's code:

<?php
error_reporting(-1);
$host = "127.0.0.1";
$port = 8000;
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Cant create the socket");
if($socket){echo $socket."\n\n"; }
$result = socket_bind($socket, $host, $port) or die("cant bind the socket into the port and the host");
if($result){echo $result."\n\n"; }
$result = socket_listen($socket, 3) or die("cant listen through the socket");
if($result){echo $result."\n\n"; }
$spawn = socket_accept($socket)or die("cant accept the incoming connections\n\n");
if($spawn){echo "an incoming connection\n\n".$spawn; } 
$input = socket_read($spawn, 1024) or die("cant read through the socket\n\n");
if($input){echo  $input."\n\n"; }
$output = strrev($input);
socket_write($spawn, $output, strlen($output)+2) or die("cant write on socket");
socket_close($socket);
socket_close($spawn);
?>

and this is the client's page:::

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	
	if(!("WebSocket" in window)){
		$('#chatLog, input, button, #examples').fadeOut("fast");	
		$('<p>Oh no, you need a browser that supports WebSockets. How about <a href="http://www.google.com/chrome">Google Chrome</a>?</p>').appendTo('#container');		
	}else{
		//The user has WebSockets
	
	connect();
		
	function connect(){
			var socket;
			var host = "ws://localhost:8000/socket/my/server.php";
			
			try{
				var socket = new WebSocket(host);
				message('<p class="event">Socket Status: '+socket.readyState);
				socket.onopen = function(){
					message('<p class="event">Socket Status: '+socket.readyState+' (open)');	
				}
				
				socket.onmessage = function(msg){
					message('<p class="message">Received: '+msg.data);					
				}
				
				socket.onclose = function(){
					message('<p class="event">Socket Status: '+socket.readyState+' (Closed)');
				}			
					
			} catch(exception){
				message('<p>Error'+exception);
			}
				
			function send(){
				var text = $('#text').val();
				if(text==""){
					message('<p class="warning">Please enter a message');
					return ;	
				}
				try{
					socket.send(text);
					message('<p class="event">Sent: '+text)
				} catch(exception){
					message('<p class="warning">');
				}
				$('#text').val("");
			}
			
			function message(msg){
				$('#chatLog').append(msg+'</p>');
			}//End message()
			
			$('#text').keypress(function(event) {
					  if (event.keyCode == '13') {
						 send();
					   }
			});	
			
			$('#disconnect').click(function(){
				socket.close();
			});

		}
		
		
	}//End connect()
		
});
</script>
<meta charset=utf-8 />
<style type="text/css">
body{font-family:Arial, Helvetica, sans-serif;}
#container{
	border:5px solid grey;
	width:800px;
	margin:0 auto;
	padding:10px;
}
#chatLog{
	padding:5px;
	border:1px solid black;	
}
#chatLog p{margin:0;}
.event{color:#999;}
.warning{
	font-weight:bold;
	color:#CCC;
}
</style>
<title>WebSockets Client</title>

</head>
<body>
  <div id="wrapper">
  
  	<div id="container">
    
    	<h1>WebSockets Client</h1>
        
        <div id="chatLog">
        
        </div>
        <p id="examples">e.g. try 'hi', 'name', 'age', 'today'</p>
        
    	<input id="text" type="text" />
        <button id="disconnect">Disconnect</button>

	</div>
  
  </div>
</body>
</html>‚Äč

but when i run the server using the shell with the current code:

php htdocs\socket\my\server.php -q

and i run the client's code, the output is nothing.. please run the code and see the output and tell me how to solve this problem... i really need to get a solution for it...

i need it for my school's science fair...

any help will be highly appreciated!!

1
Contributor
1
Reply
4
Views
6 Years
Discussion Span
Last Post by Awah Mohamed
0

hey guys, come on!!! no one within the whole 900,000 member knows the answer of this problem???? anyway, i am still waiting for your help and i am also still trying to solve it my self

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.