管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket. ?. p! c& s4 p
上代码 前端 观看页面 - <script type="text/javascript">6 J- I( d0 k3 ^2 O% u$ C& D4 O
- var ws = new WebSocket('ws://192.168.0.150:9502');. E# h- p. x' c* z; E' }( P% J4 n
6 @3 L* e. z: M0 {7 d- ws.onopen = function() {
, w& h6 {" [4 k) {8 t2 l- ` - console.log("连接成功");% h) ^: @0 v c
- };
9 S( y) M' U+ k( t1 w - ws.onmessage = function(e) {% L {. o, _, U! G" {
- console.log(e);
5 Z0 e( X8 K6 w; c7 ^- r9 S6 M
9 m% m* |- U( o% B8 v P- _- var data = e.data;
1 i# u& N9 V) W - document.getElementById('player').src=data;
K( k' c0 [# \- m6 k' e; K! C - };
_! R0 v- \6 j# p - ws.onerror = function() {
, I+ k/ b; E& c0 X - console.log("关闭连接");' Z. y3 W8 d1 X- I$ {8 M- F
- };
3 N1 |. K8 L. ^ - </script>
复制代码录像页面
6 J ~4 M( m! _" M4 u最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>6 m" W6 D" h; S4 Q
4 _+ e6 H- c' \0 Z; c3 M- 2 L9 w: J* n H4 ? |6 N" C4 a
4 l4 S; A% \2 ~9 v% G- <script type="text/javascript" charset="utf-8"> H" v1 e) d7 K6 `. z' V4 n
( _4 M% ?9 l1 _4 L6 n _! W; p-
8 |! M0 L( a/ X, I* D - var socket = new WebSocket('ws://192.168.0.150:9502');. u# p5 n% j# ^/ H
- //socket.send("嗨我登陆了");
& C9 _$ v0 n% r: G
+ l7 t. ~- R9 H9 f/ V; j- var back = document.getElementById('output');
$ C2 F' _( z- W k! W - var backcontext = back.getContext('2d');9 L/ H8 I. A5 e$ Y+ g6 `
- var video = document.getElementsByTagName('video')[0];
3 o& X% N4 |0 A* m - / Z' [" }9 Q0 F. M5 E: A
- var success = function(stream){* I! X, c4 ^. b2 z
- video.src = window.URL.createObjectURL(stream);2 B, r/ t: E: w8 ~) s
- }; |% C B1 ^1 n
- - g7 p% D" [$ ]" t% W, T2 [) J* h, V
- socket.onopen = function(){) w8 S8 d6 Z2 G6 d3 P& G
- draw();; s4 |, z. S* B" x. \& m' f' S2 I
- }
* {4 j" N* @# |% P" A+ W - 4 K! f. ^, K8 Y/ X
- var draw = function(){
* g( N* k: A1 d& N+ c, ?( x" d3 _5 }3 j1 R - try{% }' J: p0 o. e4 d+ C% c
- backcontext.drawImage(video,0,0, back.width, back.height);) @* ?4 v9 W' V8 t3 b, C) M- n
- }catch(e){2 B1 r+ s2 H. o+ E p; `" b' }3 S. m' x
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {
' W, q7 r% W" h% w8 C - return setTimeout(draw, 100);
6 I1 S D. J* [' ~ - } else {2 s6 [! N8 ~. t6 j
- throw e;1 M4 h) C" W4 Z* N7 W7 i- E
- }
1 s, a& F9 R' M3 j - }
$ L+ k3 L# {1 b - if(video.src){& S8 l' G" C" Q% q
- socket.send(back.toDataURL("image/jpeg", 0.5));
l# o3 X# G1 G' ~9 a - }
. l' z \* N/ @ - setTimeout(draw, 100);2 Q" _9 `: f7 j3 b
- }! x9 T& y$ o& v' h/ x, x% c. j
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
% c, b# i$ d. o5 \! R - navigator.mozGetUserMedia || navigator.msGetUserMedia;7 Q T3 u& s l- D/ l: v
- navigator.getUserMedia({video:true, audio:false}, success, console.log);
! P/ y/ p- L7 @: Y! o6 r - </script>
复制代码- php3 g$ F& c8 C. d j
* ~) l6 {" {9 {/ H0 r- $server = new swoole_websocket_server("0.0.0.0", 9502);
" y0 U8 B! `4 F+ V0 D. H; i) b - 4 Y$ x) ~) v8 E7 ~8 ]3 t
- $server->on('open', function (swoole_websocket_server $server, $request) {2 w3 Q2 t" P( s7 b {
+ K, z' _! l) v- echo "新用户id:{$request->fd}加入了\n";
, A3 O" C5 ~' \4 a - // echo "server: handshake success with fd{$request->fd}\n";+ _7 R3 B, Y( B
- });5 @. l" D9 h( A4 c3 H4 e
- 7 ]! U2 n, ~. K. D. e. h9 X
- $server->on('message', function (swoole_websocket_server $server, $frame) {
+ u& C: C- d% @4 ^ - ' ?# n: i8 E) T$ H& L3 i
- //循环所有数据( [& H0 W4 v& Y' L5 D. e7 O* u8 i
- foreach($server->connections as $fd) {
3 _: i! N4 b2 v" v9 S0 k - //返回数据
6 n1 ~ @$ x M; L - $server->push($fd, $frame->data);* M7 S' C8 u( _4 Q0 X+ S Q
- } {2 j' e; c m; ]. `7 w+ P! Y' `
- 6 m: }* {6 b0 D+ r
- });
0 q9 @ v" z4 t0 E+ J7 C - ( ]9 {3 N+ z: o& E
- $server->on('close', function ($ser, $fd) {
$ g! c# b, U8 ~5 r1 b" k' M - echo "用户id: {$fd} 退出\n";/ M+ ^9 S& Y: P0 E0 i: ?' M
- });, @1 _* s6 y1 l+ H& y6 z' h* y
- 3 y* ~0 ], K# ?5 N8 M y0 @% N
- $server->start();
复制代码 6 P1 S0 {9 p* b* o8 U' r, _8 S+ X
: I* |# p5 v' [+ ~5 _$ g6 ^
4 M+ J% Y+ ], g; Q% d6 j
1 p$ F5 G0 K G$ f7 \3 q( [. G4 M& v7 I. ~# f, l9 P; k# P
/ v: {: Y2 `; F8 ^, I
. U) [( v8 A: w' G: s8 k [
5 F T: e. u0 H+ l3 F8 Z7 p6 ]! f3 k# k: m7 S
3 u3 R! G9 v; R% F x2 m/ U6 D
% T% Y7 E8 o" C% j; h) [* a1 T
7 U8 W5 \. D3 @9 k d6 x8 i _) [$ A |
|