首页 前端知识 windows下使用php socket 和 html5 websocket实现服务器和客户端之间通信_php websocket执行windows命令

windows下使用php socket 和 html5 websocket实现服务器和客户端之间通信_php websocket执行windows命令

2024-07-02 23:07:25 前端知识 前端哥 54 388 我要收藏

$cycle[] = t h i s − > s o c k e t ; s o c k e t _ s e l e c t ( this->socket; socket\_select( this>socket;socket_select(cycle, $write, $except, null);

foreach ($cycle as KaTeX parse error: Expected '}', got 'EOF' at end of input: sock) { if (sock == $this->socket) {
t h i s − > a c c e p t [ ] = s o c k e t _ a c c e p t ( this->accept[] = socket\_accept( this>accept[]=socket_accept(sock);
a r r = a r r a y _ k e y s ( arr = array\_keys( arr=array_keys(this->accept);
k e y = e n d ( key = end( key=end(arr);
t h i s − > h a n d s [ this->hands[ this>hands[key] = false;
}else{
l e n g t h = s o c k e t _ r e c v ( length = socket\_recv( length=socket_recv(sock, $buffer, 204800, null);
k e y = a r r a y _ s e a r c h ( key = array\_search( key=array_search(sock, t h i s − > a c c e p t ) ; i f ( ! this->accept); if (! this>accept);if(!this->hands[$key]) {
t h i s − > d o h a n d s h a k e ( this->dohandshake( this>dohandshake(sock, b u f f e r , buffer, buffer,key);
}else if($length < 1){
t h i s − > c l o s e ( this->close( this>close(sock);
}else{
//接收从客户端发送的内容 解码
$data = t h i s − > d e c o d e ( this->decode( this>decode(buffer);
print_r(“\nThis is the data received from client,”.$data);
//服务器端的内容 编码
d a t a = " T h i s i s t h e d a t a s e n t b y s e r v e r " . data = "This is the data sent by server". data="Thisisthedatasentbyserver".i.“, so smart”;
$data = t h i s − > e n c o d e ( this->encode( this>encode(data);
print_r( d a t a ) ; / / 将信息传递给从机 f o r e a c h ( data); //将信息传递给从机 foreach ( data);//将信息传递给从机foreach(this->accept as KaTeX parse error: Expected '}', got 'EOF' at end of input: … socket\_write(client, d a t a , s t r l e n ( data ,strlen( data,strlen(data));
}
$i++;
}
}
}
sleep(1);
}
}/* end of start*/

/**
* 首次与客户端握手
*/
public function dohandshake($sock, $data, $key) {
if (preg_match(“/Sec-WebSocket-Key: (.*)\r\n/”, $data, $match)) {
r e s p o n s e = b a s e 64 _ e n c o d e ( s h a 1 ( response = base64\_encode(sha1( response=base64_encode(sha1(match[1] . ‘258EAFA5-E914-47DA-95CA-C5AB0DC85B11’, true));
$upgrade = “HTTP/1.1 101 Switching Protocol\r\n” .
“Upgrade: websocket\r\n” .
“Connection: Upgrade\r\n” .
"Sec-WebSocket-Accept: " . KaTeX parse error: Undefined control sequence: \n at position 15: response . "\r\̲n̲\r\n"; socket\…sock, u p g r a d e , s t r l e n ( upgrade, strlen( upgrade,strlen(upgrade));
t h i s − > h a n d s [ this->hands[ this>hands[key] = true;
}
}/*dohandshake*/

/**
* 关闭一个客户端连接
*/
public function close($sock) {
k e y = a r r a y _ s e a r c h ( key = array\_search( key=array_search(sock, t h i s − > a c c e p t ) ; s o c k e t _ c l o s e ( this->accept); socket\_close( this>accept);socket_close(sock);
unset( t h i s − > a c c e p t [ this->accept[ this>accept[key]);
unset( t h i s − > h a n d s [ this->hands[ this>hands[key]);
}

/**
* 字符解码
*/
public function decode($buffer) {
$len = $masks = $data = $decoded = null;
l e n = o r d ( len = ord( len=ord(buffer[1]) & 127;
if ($len === 126) {
m a s k s = s u b s t r ( masks = substr( masks=substr(buffer, 4, 4);
d a t a = s u b s t r ( data = substr( data=substr(buffer, 8);
}
else if ($len === 127) {
m a s k s = s u b s t r ( masks = substr( masks=substr(buffer, 10, 4);
d a t a = s u b s t r ( data = substr( data=substr(buffer, 14);
}
else {
m a s k s = s u b s t r ( masks = substr( masks=substr(buffer, 2, 4);
d a t a = s u b s t r ( data = substr( data=substr(buffer, 6);
}
for ($index = 0; i n d e x < s t r l e n ( index < strlen( index<strlen(data); $index++) {
$decoded .= d a t a [ data[ data[index] ^ m a s k s [ masks[ masks[index % 4];
}
return $decoded;
}

/**
* 字符编码
*/
public function encode($buffer) {
l e n g t h = s t r l e n ( length = strlen( length=strlen(buffer);
if(KaTeX parse error: Undefined control sequence: \x at position 27: …25) { return "\̲x̲81".chr(length).KaTeX parse error: Expected 'EOF', got '}' at position 10: buffer; }̲ else if(length <= 65535) {
return “\x81”.chr(126).pack(“n”, l e n g t h ) . length). length).buffer;
} else {
return “\x81”.char(127).pack(“xxxxN”, l e n g t h ) . length). length).buffer;
}
}

}/* end of class Server_socket*/

$server_socket = new Server_socket(‘127.0.0.1’,9000,20);
$server_socket->start(); sleep(2);
?>


主要的函数是start()函数,当中写了注释内容,可以根据自己的需要进行修改


3、客户端JavaScript websocket



websocket 发送 ```

这里客户端页面加载后就与服务器端进行连接,如果成功之后,就会执行onopen,如果服务器发送内容,执行onmessage,其中event是服务器发送的对象。查看更多

4、运行步骤
1)下载源代码至本地
2)修改run.bat文件存放路径
3)双击运行脚本
4)打开浏览器,我是放在Apache的www文件夹下面的,输入http://127.0.0.1/websocket/wobsocket.html
5)如果报错
这里写图片描述
表示Server_socket.php 没有执行,也可能是火狐浏览器的websocket没有打开。
打开火狐浏览器的websocket方法
1)地址栏输入about:config
2)回车,搜索框输入websocket,将与websocket有关的内容全部打开,重启浏览器。
这里写图片描述
6)执行效果
这里写图片描述
左边的cmd是执行run.bat的脚本
右边的控制台是客户端执行的有关打印
可以看出,客户端发送的信息被服务端接收,并且服务端发送的内容客户端也可以被接收。

之所以要做这个是有关实现团队前端主机创建成员,从机加入后,推送消息给主机,是哪一个成员加入,以及主机点击开始操作之后,主机和从机同时进入操作页面。

另外,还可能后期还会有操作期间的会话,其实这个已经可以简单实现,不过问题是需要区分团队,每一个团队有一个聊天室。以及发送消息区分每一个用户。可能后期会参考打开,以及打开

给大家的福利

零基础入门

对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。

同时每个成长路线对应的板块都有配套的视频提供:

在这里插入图片描述

因篇幅有限,仅展示部分资料

转载请注明出处或者链接地址:https://www.qianduange.cn//article/13824.html
标签
windows
评论
发布的文章

读魏书生的心得体会

2024-07-03 14:07:10

jQuery 选择器

2024-05-12 00:05:34

Vue中public/assets目录区别

2024-07-02 23:07:29

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!