search
Homephp教程php手册PHP实现Socket侦听消息简单示例

PHP实现Socket侦听消息简单示例

Jun 06, 2016 pm 07:57 PM
phpsocketaccomplishprocessinformationExampleSimple

流程: 服务器端不断侦听socket请求 客户端向服务器发送一个含有独特key的socket请求 服务器接收到该请求,获得客户端的socket,进行一次“握手”,即向客户端发送含有客户端key的特定消息 客户端接收到服务器发送的消息,表明连接成功,可以向服务器发消息

流程:

服务器端不断侦听socket请求

客户端向服务器发送一个含有独特key的socket请求

服务器接收到该请求,获得客户端的socket,进行一次“握手”,即向客户端发送含有客户端key的特定消息

客户端接收到服务器发送的消息,表明连接成功,可以向服务器发消息

服务器握手成功后,开始侦听该客户端发送的消息,并且对消息进行解码

前端使用html5的 WebSocket API


浏览器打开index.php

后台执行php server.php(首先要配置php命令可用)


index.php:






<script>
	
	so = new WebSocket(&#39;ws://localhost:8080&#39;);
	
	so.onopen = function()
		{
			if (so.readyState==1)
			{	alert(&#39;socket connected&#39;);
				so.send(&#39;hello world&#39;);
			}
			
		};
		
	so.onclose = function()
		{
			alert(&#39;socket closed&#39;);
		};
		
	

</script>

server.php

<?php $sk = new Sock("localhost", 8080);

$sk->run();

class Sock
{
	public $master;
	
	public function __construct($address, $port)
	{
		//create a socket
		$this->master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1);
		
		//bind socket with address and port
		socket_bind($this->master, $address, $port);
		
		//listen!
		socket_listen($this->master);
		
		echo ('Server Started : '.date('Y-m-d H:i:s') ."\n");
		echo ('Listening on   : '.$address.' port '.$port ."\n");
	}
	
	function run()
	{
		while (true)
		{
			if ($client = socket_accept($this->master))
			//if a socket is accepted
			{
				$len = 0;
				$buffer = '';
				
				do{
					$l = socket_recv($client, $buf, 1000, 0);
					$len += $l;
					$buffer .= $buf;
				}
				while($l == 1000);
				
				//handshake
				$buf  = substr($buffer,strpos($buffer,'Sec-WebSocket-Key:')+18);
				
				$key  = trim(substr($buf,0,strpos($buf,"\r\n")));

				$new_key = base64_encode(sha1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true));

				$new_message = "HTTP/1.1 101 Switching Protocols\r\n";
				$new_message .= "Upgrade: websocket\r\n";
				$new_message .= "Sec-WebSocket-Version: 13\r\n";
				$new_message .= "Connection: Upgrade\r\n";
				$new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";

				socket_write($client, $new_message, strlen($new_message));
				//handshake over
				
				while(true)
				//now listen the message sent by client
				//because we know client is about to sent a message refer to index.php
				{
					$buf = '';
					$len = 0;
					$buffer = '';
					
					do{
						$l = socket_recv($client, $buf, 1000, 0);
						$len += $l;
						$buffer .= $buf;
					}while($l == 1000);
					
					echo $this->decode($buffer)."\n";
					
					break;
				}
				
			}
		}
	}
	
	function decode($str){
		$mask = array();
		$data = '';
		$msg = unpack('H*', $str);
		$head = substr($msg[1], 0, 2);

		if ($head == '81') 
		{
			$len = substr($msg[1], 2, 2);
			$len = hexdec($len);

			if(substr($msg[1], 2, 2) == 'fe')
			{
				$len = substr($msg[1], 4, 4);
				$len = hexdec($len);
				$msg[1] = substr($msg[1],4);
			}
			else if(substr($msg[1], 2, 2) == 'ff'){
				$len = substr($msg[1], 4, 16);
				$len = hexdec($len);
				$msg[1] = substr($msg[1], 16);
			}

			$mask[] = hexdec(substr($msg[1], 4, 2));
			$mask[] = hexdec(substr($msg[1], 6, 2));
			$mask[] = hexdec(substr($msg[1], 8, 2));
			$mask[] = hexdec(substr($msg[1], 10, 2));
			$s = 12;
			$n = 0;
		}
		else
		{
			return 'decode failed';
		}

		$e = strlen($msg[1]) - 2;
		
		for ($i = $s; $i 


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool