管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
& `+ q; g% T3 e7 X' f上代码 前端 观看页面 - <script type="text/javascript">
9 J' W# ~8 w- y: V( C! j - var ws = new WebSocket('ws://192.168.0.150:9502');) h: ?4 ~+ N7 J T# T
5 }* W- x" P |* O- ws.onopen = function() {7 ]* ~: J$ ~8 x7 I: C2 W
- console.log("连接成功");- s) G U1 U: H/ E0 P) b
- };
$ J, K# l( o9 g3 c* c - ws.onmessage = function(e) {
( [5 i1 a# M; B2 B$ U4 L% z - console.log(e);8 E, q \& `, \
! W& y0 f2 d6 R4 j: L; c/ S- var data = e.data;
+ f& j! R- W6 U - document.getElementById('player').src=data;1 P" ?- Z' ]: E( }0 D
- };
3 }, y0 y1 a4 f! X: m" U c - ws.onerror = function() {
/ D9 J7 C" J7 a/ |- b9 z: c - console.log("关闭连接");
8 S$ J% C5 G9 S" r+ g8 B6 j+ q - };
0 `9 V/ E, ^' s( J( X: p: N - </script>
复制代码录像页面
$ L' \% O8 H0 j, s* J/ r最好用火狐测试。 - <canvas id="output" style="display:none"></canvas> \4 T; Q3 `* E
/ ~; [ n7 ~/ i- . \+ E o6 b, i$ R
' G/ h5 b& u0 Z) _- <script type="text/javascript" charset="utf-8">, {+ g1 g$ u' C! G$ M- t# ?
- : G- C0 M9 Q0 p. G$ J4 w. `
-
, X( H7 p1 ~. L' r - var socket = new WebSocket('ws://192.168.0.150:9502');
) C; i- s6 U% m; p: O, w. B1 ], G: D - //socket.send("嗨我登陆了");7 _: s! N& b- O
# B. V/ L; W) |5 W2 S3 E1 B% I- var back = document.getElementById('output');
: T6 t6 e9 J8 o! _7 S* N - var backcontext = back.getContext('2d');
) t6 X) G" ~( D0 A( { - var video = document.getElementsByTagName('video')[0];9 `' I8 t0 m' c4 w
- 3 A3 R" j* Q; z7 Q7 S$ x+ m
- var success = function(stream){. ]" y5 Q3 S; |# ^
- video.src = window.URL.createObjectURL(stream);
/ q6 O G, A1 n# m3 n4 M# O( s - }
p/ r, Z' Z6 |4 R - 5 S; F4 w7 p1 u6 S# O* L2 Z3 u7 x
- socket.onopen = function(){
( d0 h+ h8 B2 P. p) K$ _4 `8 o - draw();
% ?) N8 G" C% B. c/ f - }1 M! f5 V+ H$ t3 Q. N. ]* I' M
5 f4 L% D7 [) e0 I$ X7 [; _- var draw = function(){
l" c# e5 }, C7 ~9 G - try{( D& s- ~# s8 {4 O( A9 T( n* j$ V* ^
- backcontext.drawImage(video,0,0, back.width, back.height);
0 C! { U: V+ {, P- Z - }catch(e){& o5 ?# \5 k( a; g+ t) n o' R
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {. [: g' L3 D- m3 ]) s
- return setTimeout(draw, 100);
$ o7 e3 ]: R1 R4 o0 ~5 \ - } else {
7 ~. V9 ~$ G( F w$ u$ H, k/ P, x - throw e;
2 v! P/ a* E* y+ e9 k a2 U# J - }
2 Q2 i. Z& E& _3 V) d2 k) J1 W - }
. h: M6 D" {, n - if(video.src){
; S9 K; j8 R6 x$ i( C: T - socket.send(back.toDataURL("image/jpeg", 0.5));! W' R, j- V7 D0 U; A2 U
- }8 V/ R$ X( R- S" y& R
- setTimeout(draw, 100);
4 N% L3 I/ t- u$ h/ k+ H% `: W# z - } N: b# t7 O. M
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||8 o2 h2 M+ p2 \, w: d) i9 e; }3 `
- navigator.mozGetUserMedia || navigator.msGetUserMedia;
1 ?; B* b; O* B% c2 k3 Y8 R- w - navigator.getUserMedia({video:true, audio:false}, success, console.log);
' q V2 j; d: `+ X5 G) P7 r - </script>
复制代码- php
0 T/ Y" m2 P$ ~! O - 1 s8 e! Q9 D( S% W7 e5 C
- $server = new swoole_websocket_server("0.0.0.0", 9502);& _6 D d+ [3 [: ~6 [( R: j9 \
l4 q j" c8 o1 R7 I! c1 k0 o- $server->on('open', function (swoole_websocket_server $server, $request) {' _. P% E0 ^# V% `3 e
. n# @* i2 ~$ [" l: Q4 y6 x- echo "新用户id:{$request->fd}加入了\n";
" }7 z8 P" U$ ~# W2 { - // echo "server: handshake success with fd{$request->fd}\n";
$ N( N. h0 k' M& q& U' g/ s - });
; a$ t( Y4 Y' s$ V) t - 9 N" x! A3 Q- Z! _+ g
- $server->on('message', function (swoole_websocket_server $server, $frame) {+ o' ~/ H1 p! ~% P; ^) `; a
- # d0 X0 R1 `, f( s2 J
- //循环所有数据: j; V& d& w' U! }) a
- foreach($server->connections as $fd) {, q* d& {3 F4 \. W3 D
- //返回数据
7 p1 p, F* v- V2 ~$ n$ w9 | - $server->push($fd, $frame->data);, D2 o* G) t2 c
- }
% x- @1 z; }* @8 a( y' J
) }8 V! S1 @0 T, o- });5 t h8 `2 l# Q% |7 {
9 G [( L! `) o) l, y) e8 P- $server->on('close', function ($ser, $fd) {
. z% k* Y6 w; v Y4 ~% g, R - echo "用户id: {$fd} 退出\n";; t( Y( T0 z( c4 g& Y$ S1 p. ]
- });% N8 Q2 p- p& o3 d) r3 {
2 P2 g( O) ?, c, F% W- $server->start();
复制代码
% \& ^+ \& L! S/ ?2 t/ C7 i3 p7 z& N5 |" ^2 t
5 f1 G7 n% B) m* I6 V: P. R
; j1 `. l' _3 q% R4 ]/ `
- K7 K; _/ t6 p. `6 c. G
* t8 E1 C# k+ _% o4 |; v
- h* J6 l+ `/ h# Z" b" z% U9 a" M+ w+ s5 l/ W
3 U$ K8 N: J5 q8 Y0 Y3 t
, ]2 K* A @* H* y. ]
- r4 T% c1 Q6 h' y# V# u% D8 u* _+ y8 ]( l" T! G$ L3 U
|
|