管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
" m* n# Z! v2 j5 C上代码 前端 观看页面 - <script type="text/javascript">: _8 E: F6 A& k
- var ws = new WebSocket('ws://192.168.0.150:9502');
4 Z3 g) w8 P3 j( [( U% M- L K
7 l" Z+ C, F5 k! _0 ^& d9 l& V( Z. ]- ws.onopen = function() {
# o( P3 ?3 g) {4 t! k - console.log("连接成功");
+ V0 G4 r: e& A& g3 f( o6 s: f - };, I0 Q+ U8 k3 }$ S( m& | r6 C
- ws.onmessage = function(e) {3 q* p0 o, p& M/ R/ M3 _- _
- console.log(e);( W# g* `7 V* A! @; W+ [, J$ F
; _: T2 ] X, I g/ j n- var data = e.data;0 C, [8 x! u* U
- document.getElementById('player').src=data;
& c, a# y8 c) t6 q, Y! ^ - };
2 b D& H5 Z! Z) w - ws.onerror = function() {, ^: n# I+ y5 S! ]: f, ^
- console.log("关闭连接");
# Y/ a z4 {/ v& d# B - };8 s/ n- [$ s7 Q4 u1 V
- </script>
复制代码录像页面 4 J1 J& f" x2 [. A% t5 W. q
最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>* H, x! Y+ }- C( ]% F8 Z
) d. {4 E- r* S, x7 @8 ~
0 r) F. k3 g. y& r. \) M- g- : U4 q7 Y O2 `: {7 `' C
- <script type="text/javascript" charset="utf-8">
4 g3 r' F& i( B1 S- ~ - - D. ~% y) M1 Z, @. s; @
-
; m# M6 B! |' O1 B1 u* l - var socket = new WebSocket('ws://192.168.0.150:9502');
W* B I" U4 S9 Q4 p4 j - //socket.send("嗨我登陆了");
! a5 M0 r' b3 z6 G1 |1 ~
- R' t1 b8 H, M8 g$ k- var back = document.getElementById('output');6 W/ X" r' z% U
- var backcontext = back.getContext('2d');
o0 W& M. y" J: b - var video = document.getElementsByTagName('video')[0];) D7 K8 d6 _! v" d
- & D* U1 P( k+ s6 ?
- var success = function(stream){
( @% @1 s r& Z7 n - video.src = window.URL.createObjectURL(stream);
( ?+ {0 p4 \( f- ^( m - }: x# k# q4 o' w0 v; b" o5 \; C
- 0 e0 C) H5 c' {' d( O, l
- socket.onopen = function(){
0 }, z A$ C' {5 A. @ - draw();' h7 J) f% \$ o0 G8 x. O
- }
% {8 V9 e* g9 L2 b - * K+ g, s$ S7 e! D. ?* X2 C) L; i( ]
- var draw = function(){
- ~# \1 p2 f5 e- Q1 D$ X D - try{
4 _1 j, y: Q" c+ |7 Y) [! p - backcontext.drawImage(video,0,0, back.width, back.height);
5 m( x8 |+ V; o6 _' ^! y9 s% W- E9 d - }catch(e){
- j9 j) I# _( y& H% f$ e0 p - if (e.name == "NS_ERROR_NOT_AVAILABLE") {
6 K/ v. c# X! m. Q( w+ E - return setTimeout(draw, 100);7 K3 ^% W- q( D& _8 t# u
- } else {
+ |5 J; l" @; h4 D - throw e;( _+ s2 e; y9 |
- }
/ J+ M( D9 V0 v! V) I& H - }
; u" N3 G5 @9 c1 ]/ t - if(video.src){
; q" s5 E5 @/ z& J9 Z8 H - socket.send(back.toDataURL("image/jpeg", 0.5));: i* d! \' z8 B. H
- }
" G K/ j a4 }& V B7 O7 Y - setTimeout(draw, 100);
2 T& ~! w; A% i: s5 Q, G# f/ ?, { - }
7 O# E' ^3 n; c- ^4 b5 J+ ]0 p! {" ` - navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||9 P4 R- I* M$ P: O1 J. [, c
- navigator.mozGetUserMedia || navigator.msGetUserMedia;
) j% S3 S* D9 o - navigator.getUserMedia({video:true, audio:false}, success, console.log);
% ?% i% _/ N. m2 y* f - </script>
复制代码- php; J7 H* c) v, Y7 d2 i
1 a1 o/ T$ ~' s$ O E- $server = new swoole_websocket_server("0.0.0.0", 9502);
% C( |: A Z$ ~. \4 v) ^2 a
7 `& p! `) F( x6 y4 C- $server->on('open', function (swoole_websocket_server $server, $request) {5 D- c7 W& P6 o0 J: }3 N0 S
, u) x7 ?/ k0 S6 ]# H; ~9 Y( Z+ i* b- echo "新用户id:{$request->fd}加入了\n";
. }/ T( q# u4 C; a) d/ ]+ w0 p - // echo "server: handshake success with fd{$request->fd}\n";
& F* P; n; x' R3 u - });) [- C; O' l* G1 e- f
# D! n) o/ }4 w: \8 Y) e7 ^- $server->on('message', function (swoole_websocket_server $server, $frame) {! M# ]1 g0 q/ b* h) Z. N( Z2 l- g
- . ? I( P1 t3 A: z+ o
- //循环所有数据2 i2 v3 W; N0 H' z
- foreach($server->connections as $fd) {' x }, j. j7 [
- //返回数据
! }3 J4 T: N6 Q* M4 h - $server->push($fd, $frame->data);
6 q' w& o) S) k; s8 t Q - }/ l& H' c# a% F) B
- / \4 N, n! V) ]+ ~. F C
- });
. w+ @' X) `8 R: Z h5 l8 X
* P! o, T& C4 u6 H; \; ?- $server->on('close', function ($ser, $fd) {
7 D0 Z& [5 V: G7 `4 j X - echo "用户id: {$fd} 退出\n";- k. s4 B1 U3 G7 T9 Q) `
- });
0 N! w- l- P% U$ e* W$ r. A$ a! r% |8 K8 Z
9 \) C1 `# h1 |+ l7 n' g- $server->start();
复制代码
, B, a) V# U2 I; n; Y- X. ?: D* R/ X! p
! Y& G% u: V1 ?. K5 c
2 k9 _8 @+ G' v" m
9 o6 r( l5 j5 u/ k
4 }- u/ M, y6 n% K
! v2 y" D- {/ k) u, J: ?0 ], d7 B3 d/ h" a8 A, J9 g$ V7 [5 b
* p: V1 \# z; Q
: e3 {9 m' A! h5 h5 s( h: L$ Q9 [- L
" I- Q3 u0 U7 O3 b9 ^# l1 ^
$ J! Q9 I# f5 L3 i- t1 } |
|