gpt4 book ai didi

ios - Socket.io 与 PHP websocket 握手?

转载 作者:行者123 更新时间:2023-11-29 11:43:30 26 4
gpt4 key购买 nike

我在服务器端使用 PHP 网络套接字。我目前只测试客户端和服务器之间的连接,所以我还没有配置套接字来响应特定的事件。这是基本模板的样子:

#!/php -q
<?php /* >php -q server.php */

error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$master = WebSocket("example.com",8080);
$sockets = array($master);
$users = array();
$debug = false;

while(true){
$changed = $sockets;
socket_select($changed,$write=NULL,$except=NULL,NULL);
foreach($changed as $socket){
if($socket==$master){
$client=socket_accept($master);
if($client<0){ console("socket_accept() failed"); continue; }
else{ connect($client); }
}
else{
$bytes = @socket_recv($socket,$buffer,2048,0);
if($bytes==0){ disconnect($socket); }
else{
$user = getuserbysocket($socket);
if(!$user->handshake){ dohandshake($user,$buffer); }
else{ process($user,$buffer); }
}
}
}
}

//---------------------------------------------------------------
function process($user,$msg){
$action = unwrap($msg);
say("< ".$action);
switch($action){
case "hello" : send($user->socket,"hello human"); break;
case "hi" : send($user->socket,"zup human"); break;
case "name" : send($user->socket,"my name is Multivac, silly I know"); break;
case "age" : send($user->socket,"I am older than time itself"); break;
case "date" : send($user->socket,"today is ".date("Y.m.d")); break;
case "time" : send($user->socket,"server time is ".date("H:i:s")); break;
case "thanks": send($user->socket,"you're welcome"); break;
case "bye" : send($user->socket,"bye"); break;
default : send($user->socket,$action." not understood"); break;
}
}

function send($client,$msg){
say("> ".$msg);
$msg = wrap($msg);
socket_write($client,$msg,strlen($msg));
}

function WebSocket($address,$port){
$master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
socket_bind($master, $address, $port) or die("socket_bind() failed");
socket_listen($master,20) or die("socket_listen() failed");
echo "Server Started : ".date('Y-m-d H:i:s')."\n";
echo "Master socket : ".$master."\n";
echo "Listening on : ".$address." port ".$port."\n\n";
return $master;
}

function connect($socket){
global $sockets,$users;
$user = new User();
$user->id = uniqid();
$user->socket = $socket;
array_push($users,$user);
array_push($sockets,$socket);
console($socket." CONNECTED!");
}

function disconnect($socket){
global $sockets,$users;
$found=null;
$n=count($users);
for($i=0;$i<$n;$i++){
if($users[$i]->socket==$socket){ $found=$i; break; }
}
if(!is_null($found)){ array_splice($users,$found,1); }
$index = array_search($socket,$sockets);
socket_close($socket);
console($socket." DISCONNECTED!");
if($index>=0){ array_splice($sockets,$index,1); }
}

function dohandshake($user,$buffer){
console("\nRequesting handshake...");
console($buffer);
list($resource,$host,$origin,$strkey1,$strkey2,$data) = getheaders($buffer);
console("Handshaking...");

$pattern = '/[^\d]*/';
$replacement = '';
$numkey1 = preg_replace($pattern, $replacement, $strkey1);
$numkey2 = preg_replace($pattern, $replacement, $strkey2);

$pattern = '/[^ ]*/';
$replacement = '';
$spaces1 = strlen(preg_replace($pattern, $replacement, $strkey1));
$spaces2 = strlen(preg_replace($pattern, $replacement, $strkey2));

if ($spaces1 == 0 || $spaces2 == 0 || $numkey1 % $spaces1 != 0 || $numkey2 % $spaces2 != 0) {
socket_close($user->socket);
console('failed');
return false;
}

$ctx = hash_init('md5');
hash_update($ctx, pack("N", $numkey1/$spaces1));
hash_update($ctx, pack("N", $numkey2/$spaces2));
hash_update($ctx, $data);
$hash_data = hash_final($ctx,true);

$upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" .
"Upgrade: WebSocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Origin: " . $origin . "\r\n" .
"Sec-WebSocket-Location: ws://" . $host . $resource . "\r\n" .
"\r\n" .
$hash_data;

socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
$user->handshake=true;
console($upgrade);
console("Done handshaking...");
return true;
}

function getheaders($req){
$r=$h=$o=null;
if(preg_match("/GET (.*) HTTP/" ,$req,$match)){ $r=$match[1]; }
if(preg_match("/Host: (.*)\r\n/" ,$req,$match)){ $h=$match[1]; }
if(preg_match("/Origin: (.*)\r\n/",$req,$match)){ $o=$match[1]; }
if(preg_match("/Sec-WebSocket-Key2: (.*)\r\n/",$req,$match)){ $key2=$match[1]; }
if(preg_match("/Sec-WebSocket-Key1: (.*)\r\n/",$req,$match)){ $key1=$match[1]; }
if(preg_match("/\r\n(.*?)\$/",$req,$match)){ $data=$match[1]; }
return array($r,$h,$o,$key1,$key2,$data);
}

function getuserbysocket($socket){
global $users;
$found=null;
foreach($users as $user){
if($user->socket==$socket){ $found=$user; break; }
}
return $found;
}

function say($msg=""){ echo $msg."\n"; }
function wrap($msg=""){ return chr(0).$msg.chr(255); }
function unwrap($msg=""){ return substr($msg,1,strlen($msg)-2); }
function console($msg=""){ global $debug; if($debug){ echo $msg."\n"; } }

class User{
var $id;
var $socket;
var $handshake;
}

?>

我想使用 Socket.io 从我的 Swift 客户端连接到这个套接字:

let io:SocketIOClient = SocketIOClient(socketURL: URL(string: "example.com:8080/server.php")!, config: [.log(true), .compress])

override func viewDidLoad() {
super.viewDidLoad()

self.io.on(clientEvent: .connect) { (data:[Any], ack:SocketAckEmitter) in
NSLog("Socket connected!")
}
self.io.on(clientEvent: .disconnect) { (data:[Any], ack:SocketAckEmitter) in
NSLog("Socket disconnected!")
}
self.io.connect()
}

我在我的 Mac 终端上使用 SSH 运行服务器。 PHP 网络套接字表示它正在监听连接,但它不响应 socket.io 连接。服务器和客户端都没有给我任何错误,所以我假设我错过了连接过程中的关键步骤。我想知道当您尝试连接到 PHP 网络套接字时是否可以使用 Socket.IO 快速客户端执行握手。这是兼容性问题还是我只是忘记了什么?顺便说一句,到目前为止,您可能已经注意到我对网络套接字编程还很陌生(不到一周前我发现了网络套接字),所以如果这是一个真正的菜鸟问题,请原谅。我感谢我能得到的任何帮助。谢谢!

最佳答案

socket.io != webSocket。您不能将 socket.io 客户端连接到 webSocket 服务器。 Socket.io 在 webSocket 之上添加了自己的协议(protocol)。虽然 socket.io 在幕后使用 webSocket 传输,但如果您只有 webSocket 服务器,客户端将无法连接。

您必须将 webSocket 客户端连接到 webSocket 服务器。或者,将 socket.io 客户端连接到 socket.io 服务器。

因此,在您的情况下,如果您有一个 socket.io 客户端,那么您需要为您的服务器环境获取一个 socket.io 服务器并改用它。

关于ios - Socket.io 与 PHP websocket 握手?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com