管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
. I3 k- ]- X6 i6 ^2 T' [2 ^) ^上代码 前端 观看页面 - <script type="text/javascript">0 e& G( }# Q( R9 Q0 w9 t7 z; U
- var ws = new WebSocket('ws://192.168.0.150:9502');
8 [' f/ h' G, r- d4 C; n$ N - 2 j y9 Y2 l E; F
- ws.onopen = function() {7 X) \" a4 w( P' O
- console.log("连接成功");5 m6 X) c# F2 N4 L
- };
8 J1 ?# i# c& v0 U/ P; S - ws.onmessage = function(e) {! t: Q& L9 G( H
- console.log(e);
. t+ t& A Z6 e" p: ~. i. L - ' i- F: Y/ J2 w: t
- var data = e.data;+ d& }( z7 v3 E$ @ H& D! U- G6 T. G
- document.getElementById('player').src=data;# r/ ?) _$ J) k+ |0 E
- };
$ c; [# F( ~" T3 o - ws.onerror = function() {& N; L4 u2 Z4 P" X2 I2 r7 g
- console.log("关闭连接");6 _& \5 |: n+ Y# v8 q+ O
- };
$ m3 I1 l- W$ d+ |0 u - </script>
复制代码录像页面
1 _; t/ G8 [2 s0 G) n最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>1 Z: I3 d3 h- T& R0 \8 V
! D! B, I* F7 U& j% g- 7 G) Z- _6 D9 z2 M
- . E1 l8 @" n9 W$ e- z0 e
- <script type="text/javascript" charset="utf-8">
+ Y8 x- {( J5 w
/ l7 W# K# @& s( t) E-
& S+ s8 y/ g9 a1 r( x# X - var socket = new WebSocket('ws://192.168.0.150:9502');
' S1 o8 Z" u6 } - //socket.send("嗨我登陆了");
' `5 B- F8 m8 U3 `- R - ! l# o, f0 ?0 c) n3 \) L
- var back = document.getElementById('output');- ?3 Z' l, [4 l+ F9 p2 p9 J c
- var backcontext = back.getContext('2d');
+ X2 ~: x- n% M( L+ ?, ` - var video = document.getElementsByTagName('video')[0];
- V0 |( t) y' `, [, a - % ~) h1 l8 J$ K$ B* A
- var success = function(stream){; ]' F4 I9 ~; D& q9 v
- video.src = window.URL.createObjectURL(stream);
. ]% ~0 D* |* F) f% N - }
% d- P4 D0 I2 y5 d9 f/ B
5 P$ F- ~& H4 J. o" g- [% ?9 X- socket.onopen = function(){3 ^$ ]! \+ [( v; z2 A
- draw();4 [; u2 u9 U: S5 S
- }
$ v' K. k4 I! m+ W6 u1 o$ Q# a
" O! j1 s. U6 ?0 n3 G1 }- S: V- var draw = function(){2 T/ Q0 N. l. a" y4 H8 U Z
- try{ s. }- \5 E( w
- backcontext.drawImage(video,0,0, back.width, back.height);
" j. P' L" k- j* a- T - }catch(e){
% A& t, e4 M' M( L5 g" c - if (e.name == "NS_ERROR_NOT_AVAILABLE") {7 h2 a g; j* R% I! Q }# R2 m( Q
- return setTimeout(draw, 100);
/ U+ ], n5 _% H6 _% ?, x, ~+ a - } else {: z( g- {9 g: t) [0 t
- throw e;
) i8 c" k6 n* I5 ` M4 \- S - }
6 I1 y/ c7 Y* ~* m, ^# Y - }
' r* |3 \4 D" s, W( y2 v6 c8 j - if(video.src){$ K6 E1 g8 [4 e+ a
- socket.send(back.toDataURL("image/jpeg", 0.5));3 k$ ~0 r, s! P/ J$ {
- }
: b D- q& X. f2 R - setTimeout(draw, 100);
/ @# C! ~& M0 G L4 x9 q1 b, c, y3 r - }! B( Q+ ]) p% { E
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
5 r# l' |. m3 v# W, W, y0 u - navigator.mozGetUserMedia || navigator.msGetUserMedia;$ A5 `4 l# o8 v( j' [5 S
- navigator.getUserMedia({video:true, audio:false}, success, console.log);
* C% ?4 m( H$ l3 ^: @) r - </script>
复制代码- php
) C- Y2 | ^1 ]6 k3 Z4 |" {; T' w
& d" i2 U6 c; R. C- $server = new swoole_websocket_server("0.0.0.0", 9502);
1 y6 `- b: {( ? - 9 v7 O% k- E2 b8 d$ R1 ^
- $server->on('open', function (swoole_websocket_server $server, $request) {
& I: Y- z( \+ \6 n! \ - 1 Q3 q8 e, m# f
- echo "新用户id:{$request->fd}加入了\n";
8 D/ Z1 ^+ A1 _+ W7 p* { - // echo "server: handshake success with fd{$request->fd}\n";
& ` [/ }( c5 r3 \ @1 } - });/ b w% m X7 b) a3 H9 V) a
8 ~: _1 ~" [, w$ o- $server->on('message', function (swoole_websocket_server $server, $frame) {
# _( [) H; j6 G# }' v$ a
~; Y9 x) G S4 \- //循环所有数据
R) w) i- }! |0 n. ] - foreach($server->connections as $fd) {2 w( N; @2 F2 |6 J2 N- w
- //返回数据/ `6 b. Q) F% n% ~6 g: b+ k$ V* j
- $server->push($fd, $frame->data);
, E7 }6 L# }; |1 q' L( e( I - }
6 t2 g& I- p6 L$ U, v% Q
( k. n/ P( o; }/ p# b- });
; P5 Q5 v8 \; @: x7 Q5 D' b3 z
7 ^$ @1 O6 E& }& l- $server->on('close', function ($ser, $fd) {
9 b2 Z K; R( z( | - echo "用户id: {$fd} 退出\n";
" B( e) w9 G* A - });$ p9 ?* L; i H, d8 Y# U1 {
- " }4 {$ C( H) c# z2 L
- $server->start();
复制代码 / k# i! K8 x2 e1 _
; i- O9 I8 m6 Q& R9 E
- Q* n3 [8 R6 q% Q6 c8 u1 W! ^
% ^0 n' o& v' L( h6 M
$ \0 [5 M+ e* O6 g% S
8 \7 f. E0 H. ?9 r" ~8 z1 C
4 p! `3 N" T1 |$ j5 H4 @& r) |4 T! `* P/ A/ ?8 ~ t3 n' Q
# ?. h' e8 D/ u
3 S# n* u8 W$ q! U; R6 |, a* P1 g9 z O$ ^; h0 E
, O! }% n. }" e" Z. ]3 C( S6 _3 h |
|