如何用webrtc实现一个p2p文件发送的代码?
网友回复
发送文件我们先实现p2p发送消息,我们用php swoole搭建一个信令服务器
<?php $userlist = []; $server = new Swoole\Websocket\Server("0.0.0.0", 9502, SWOOLE_BASE, SWOOLE_SOCK_TCP | SWOOLE_SSL); $server->set([ 'ssl_cert_file' => '/data/cert/6284283_web.debug.only.bfw.wiki.pem', 'ssl_key_file' => '/data/cert/6284283_web.debug.only.bfw.wiki.key', ]); $server->on('open', function($server, $req) { $_get = $req->get; $_username = $_get['username']; global $userlist; $userlist[$_username] = $req->fd; echo "connection open: {$req->fd}{$_username}\n"; }); $server->on('message', function($server, $frame) { $data = json_decode($frame->data, true); //消息类型 $type = $data["type"]; $_ret = ["type" => $type]; //to user $toUser = $data["toUser"]; $fromUser = $data["fromUser"]; $msg = isset($data["msg"])?$data["msg"]:""; //msg $sdp = isset($data["sdp"])?$data["sdp"]:""; //sdp $iceCandidate = isset($data["iceCandidate"])?$data["iceCandidate"]:""; //ice //对方挂断 if ("hangup" == $type) { $_ret['fromUser'] = $fromUser; $_ret['msg'] = "对方挂断"; } //视频通话请求 if ("call_start" == $type) { $_ret['fromUser'] = $fromUser; $_ret['msg'] = "1"; } //视频通话请求回应 if ("call_back" == $type) { $_ret['fromUser'] = $fromUser; $_ret['msg'] = $msg; } //offer if ("offer" == $type) { $_ret['fromUser'] = $toUser; $_ret['sdp'] = $sdp; } //answer if ("answer" == $type) { $_ret['fromUser'] = $toUser; $_ret['sdp'] = $sdp; } //ice if ("_ice" == $type) { $_ret['fromUser'] = $toUser; $_ret['iceCandidate'] = $iceCandidate; } if ($toUser != "") { global $userlist; if (!isset($userlist[$toUser])) { $_ret['msg'] = "Sorry,呼叫的用户不在线!"; $_ret['type'] = "call_back"; $_ret['fromUser'] = "系统消息"; $_senddata = json_encode($_ret); $server->push($frame->fd, $_senddata); } else { foreach ($userlist as $key => $val) { if ($key == $toUser) { $_senddata = json_encode($_ret); echo "send message: {$_senddata}\n"; $server->push($val, $_senddata); } } } } // echo "received message: {$frame->data}\n"; }); $server->on('close', function($server, $fd) { echo "connection close: {$fd}\n"; }); $server->start();在使用html实现
点击查看全文