管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
! ^1 m* t: p. s" u% v上代码 前端 观看页面 - <script type="text/javascript">8 J, L9 ~& ]' ?, S6 [9 j
- var ws = new WebSocket('ws://192.168.0.150:9502');7 W& z0 \ w. h& j' S! h
@% @9 O9 y& s- ws.onopen = function() {" _- Y+ r3 Z( }
- console.log("连接成功");2 Z" d' ~# c9 G& \
- };, R, c% B) k* M! e# P5 v0 j
- ws.onmessage = function(e) {
2 t& x7 \) v+ L5 b8 N7 D - console.log(e);
+ E Q# i0 k& ~4 Z& W. k% Z
! m' \6 O/ b, G- var data = e.data;
. f9 \8 x' w3 o - document.getElementById('player').src=data;, t3 e& ^; M! l/ ]6 q
- };: ^, Z! @. j3 `$ f8 y1 w
- ws.onerror = function() {
: U4 g3 H7 g. Y( p) @2 U1 t - console.log("关闭连接");1 F1 V x+ w5 f8 s6 B
- };
* V0 Z9 b( i& M8 O" _ - </script>
复制代码录像页面 8 f+ W1 b* w8 y+ h8 o
最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>
2 Y3 L) ]' |- Y9 m" n - * [3 a# k9 D+ W9 l$ p' ~
- ) m8 w8 A. Y' f$ g& B
. H K+ ?! W H& F w5 V- <script type="text/javascript" charset="utf-8">. F( z+ j; Q; @ u2 t2 f" T
- 9 B* D) Q. F5 ~
-
9 g; ]* d$ G! q. P$ J# J; O - var socket = new WebSocket('ws://192.168.0.150:9502');
0 T+ {; ^9 L! u3 Z - //socket.send("嗨我登陆了");
& f |% l3 K4 Y# R - ) p/ e" X# a% R
- var back = document.getElementById('output');
+ {* [6 B5 H R: X) n - var backcontext = back.getContext('2d');
- j2 A4 j M; h" M) j - var video = document.getElementsByTagName('video')[0];' |8 c# o" T1 {
- / B* n% R2 G2 S; n. A7 Q) X
- var success = function(stream){
: }7 _, `1 j& w' i7 i - video.src = window.URL.createObjectURL(stream);
; ?* A9 O% C7 d, ]* h2 O( E' S - }
- [+ M8 ~, |+ l
$ Y' G1 n1 x- K% z% H. @3 P- socket.onopen = function(){
& p- Q( X! I* S8 `$ V. S- b l - draw();) \2 G" D' ?# Y; U8 `
- }
4 \! W( |+ R& }" Y1 X - 1 Z9 k6 ^: e+ ~ x+ M$ J
- var draw = function(){
6 ^# j0 h( M. _ - try{
5 M( b- a: X" b! p - backcontext.drawImage(video,0,0, back.width, back.height);3 E2 G; t( I9 w3 D N* A6 e, P4 P
- }catch(e){# b5 k: }% a* b b- F' j
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {- q" K j. g) [6 j( `7 ~
- return setTimeout(draw, 100);
: j5 W0 d# t+ ~3 q7 A6 t. r - } else {
0 \% O( y- a7 H5 s - throw e;
% `5 z1 r* z! f) `/ H( W - }3 W8 e S8 U+ }7 Q
- }
6 `/ t7 s+ G& H* K: e - if(video.src){
* M+ ^# K9 D" x3 i( K' Q8 m - socket.send(back.toDataURL("image/jpeg", 0.5));' e8 O$ h# j; F5 O2 q
- }
8 z. R' p0 t9 j" w - setTimeout(draw, 100);! ~. {! s, H- l
- }: y% B' |3 ]: q4 P. }: d& y% G8 V) g+ h
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||7 J3 A& K9 b6 K- F9 E4 P3 h- Z
- navigator.mozGetUserMedia || navigator.msGetUserMedia;
7 z% y2 [ y0 ^1 ?5 P - navigator.getUserMedia({video:true, audio:false}, success, console.log);
8 f( R6 O {/ \& u k) u5 h! h - </script>
复制代码- php" l8 k% ]) {5 o
- : q& ?4 M! D. p
- $server = new swoole_websocket_server("0.0.0.0", 9502);
- D# t. j' w+ _& @# k5 s- C. C - / S; R/ t& x) f) ^# w
- $server->on('open', function (swoole_websocket_server $server, $request) {
/ j( |9 u2 s3 K6 W! n - * Q* E" w- Y1 |, n& `
- echo "新用户id:{$request->fd}加入了\n";
7 b1 p5 K$ V8 s, m1 r' @. j - // echo "server: handshake success with fd{$request->fd}\n";
Y2 V# B1 ^3 {3 g e6 ?' { - });
1 `9 a+ W% O/ U! u, X+ F, p
. e3 u2 g7 ?* O' L i5 g( J6 e3 h; y- $server->on('message', function (swoole_websocket_server $server, $frame) {' W9 ~1 l) \2 r$ x: @1 j* t2 u
" w. H4 t, Z+ A/ Z/ W1 d) k L7 ~- //循环所有数据. H" m* G* X/ a; a8 f
- foreach($server->connections as $fd) {! n2 J" z! w8 y3 T
- //返回数据
! Z2 u8 y# F7 S) [0 F$ Y - $server->push($fd, $frame->data);
) q& q- X( j9 [+ X! {9 B( A - }. W; o3 U% ^9 }9 L4 k, B$ q" y" A
- 9 t* O% n- Y* b1 c e
- });" l; h, m: e6 I
]3 z" u+ c5 J3 C9 a- $server->on('close', function ($ser, $fd) {
% `8 a* v' d+ u - echo "用户id: {$fd} 退出\n";: {0 V/ z& V4 W. G- @ H
- });: C$ v3 B z: e. ^% m4 w# n
, W: j0 b, b: y8 \& H- $server->start();
复制代码 6 c; I/ O/ J3 L- y8 E/ |) L% J) p
! P' j" U0 L4 `# ?* k: ]# e0 Z1 b, `& t7 z0 k h9 `! q
; ^! H( Y: j. d& e; B; n
1 `; P1 M1 V# U, I/ R& L- q) J$ q' x0 M" T! d! D d
! q& W6 B% k' e7 w: G
( ~6 \$ Q2 j$ H* J) z& }+ F! h/ g' z1 t$ ]2 @7 X+ D3 O5 F
* j* c% H3 M" w4 f# }
! a- G" D/ u. F" V" ~* S# Y* T j2 T) h6 s- w
|
|