管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
) J- H: ^ a; e5 m( I上代码 前端 观看页面 - <script type="text/javascript">( f. ~' A5 e; v3 }8 N$ l, z
- var ws = new WebSocket('ws://192.168.0.150:9502');
6 A# p* B; ]0 C1 X+ X3 t - : F ^! R8 A' S9 S: e8 M
- ws.onopen = function() {- r! B E* R* q, g0 x
- console.log("连接成功");
3 [4 e( M w. S/ ^+ @/ A3 m - };
* I/ w7 _3 L5 d" K - ws.onmessage = function(e) {& p7 g5 e) c0 i$ e1 L1 `
- console.log(e);" n# p& Z" x" {: {: L" H$ q
- 9 m+ G5 O8 S, q+ e1 H
- var data = e.data;
% o) {4 A3 M0 K- }5 `- q. g$ h9 z- P" z - document.getElementById('player').src=data;
: l* w' `) N" Y# o% a. H! ] - };
- H! U! M5 t2 T8 l - ws.onerror = function() {
$ U6 Y6 K& E. Z9 ~ - console.log("关闭连接");
$ t- V T( m, I, U% L$ V - };
8 o% j! y0 Z8 f l+ V - </script>
复制代码录像页面
! i( J/ D1 G, \: P2 e最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>
. @" b- d( a; n
3 z, E- a5 T2 G
# G- Y6 R1 ^9 C) y
6 m& S. j) E6 q. _/ @$ }2 ^/ v' l- <script type="text/javascript" charset="utf-8">
2 Q! f$ }) F8 k3 G6 b2 K
4 N+ m3 m% v- o- v+ r- 6 I/ n4 A. s8 p6 J# C, O
- var socket = new WebSocket('ws://192.168.0.150:9502');
3 i! ^1 ~4 Z* {1 w1 ~# m0 d) ~ - //socket.send("嗨我登陆了");/ S0 C3 m9 o; J. V. h/ |
- : X8 ^; s( w& Z' U" M
- var back = document.getElementById('output');% `3 A( n& \/ n# I8 d4 I3 d( ?8 a
- var backcontext = back.getContext('2d');
, G" e2 d9 f) \! V" \ - var video = document.getElementsByTagName('video')[0];
5 Y) M5 }4 y% m- J - q/ }5 h; q$ O; f1 K4 ~5 g1 D
- var success = function(stream){
1 f1 ]& _# s1 m8 E6 N - video.src = window.URL.createObjectURL(stream);
* _" h- |: K! X! O+ S0 V0 a - }
! ~1 X& u9 ?5 S4 L$ R- B; \5 G - & q( c9 M3 O7 x0 H
- socket.onopen = function(){
3 w! T: n. F- P% D t9 _ - draw();0 M; o. Z8 g3 K1 Z F4 b5 I
- }
0 i l, h( Y+ I* [' d+ c9 N
( X& h) Z+ F! r6 A5 S5 I- var draw = function(){" g0 j# Y6 M( _
- try{
: N4 z/ A' }% @6 P* B: R - backcontext.drawImage(video,0,0, back.width, back.height);' c% d* J5 h0 M& h: |4 \: @
- }catch(e){/ _$ R5 U; J6 Z# N E
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {5 r; S, G0 ]4 Q y
- return setTimeout(draw, 100);6 N; p# U5 \( Z1 ?; D
- } else {+ a# S, P& _$ H- e
- throw e;
2 H* {: M4 M$ R3 g4 Q! ` - }( _5 ~& Q f; k A+ Z) X9 S1 Z! N
- }2 ^, S8 @" D. L; U# S
- if(video.src){
( x3 |! e7 p7 k! ]* m; \ - socket.send(back.toDataURL("image/jpeg", 0.5));5 N, O0 q: P2 [1 T& N
- }. F& M6 z) p4 m' J
- setTimeout(draw, 100);
. k- P6 J* t j0 ^" A - }: m0 H; Q- c- Y! {
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||( ?) u8 n" ]9 e5 E u2 O: J) q
- navigator.mozGetUserMedia || navigator.msGetUserMedia;
5 y4 _, p% m! x - navigator.getUserMedia({video:true, audio:false}, success, console.log);1 R, g/ K7 {4 V$ a2 |
- </script>
复制代码- php6 u- }3 x( \" ?. V
- . }8 Q& E: m! K6 C/ ^% d
- $server = new swoole_websocket_server("0.0.0.0", 9502);
( S% M4 f) H6 E- l& Q( b* a0 ^/ i
+ ~7 \) Y: z# @' a+ d- $server->on('open', function (swoole_websocket_server $server, $request) {# w$ ^; S$ k+ t$ C
- # r- k* T0 V# d1 U$ n8 J7 E' S
- echo "新用户id:{$request->fd}加入了\n";9 s# Y; Y8 B) a2 x) {9 T4 r/ ]
- // echo "server: handshake success with fd{$request->fd}\n";0 g5 O# q; R& j! l h
- });
. ^8 }3 q1 e3 I9 G
& G+ y8 {& D5 R9 s# n% a- $server->on('message', function (swoole_websocket_server $server, $frame) {" O3 J( C: U, g$ n8 O- T. b) g
- , i1 [ f2 ^' \) d" {
- //循环所有数据
& ^8 _$ i1 F2 v9 F1 l# A4 `8 h - foreach($server->connections as $fd) {% @; H, X7 Z! s& Q, c; @
- //返回数据; b1 V# y/ q7 \
- $server->push($fd, $frame->data);
: {/ l( ~5 ?4 c# U* q0 o - }0 d) S% m7 A, a: ^2 M/ H: O) _
- / X* ^" F! l. a) |1 B
- });
1 n' \! n* x1 T h c4 m
: E( G( P/ a2 Y- $server->on('close', function ($ser, $fd) {8 S% |+ J. m. Y" } Q
- echo "用户id: {$fd} 退出\n";. W) R1 H# E7 F" S7 l: J
- });
/ k8 E. d, D" E# n8 s6 U - - m4 E6 l) V" f- i8 ~: v9 }1 U
- $server->start();
复制代码
! z% w0 e- w! @2 B* w6 d5 Y# P7 y u) p) J; p6 E
3 I! B8 T9 K) }7 o7 N1 L5 @! k( T3 U0 i- A0 B
$ ~( i, C- }5 M9 U" a5 Q, I3 I2 B" w( _. \3 r5 f
+ i8 w+ [& s% K) B" O) I5 c
- V+ a8 f3 z$ Q5 o- V5 L9 l
- N; L* K. ^( P
: J4 O! Y# `# q, J, K7 W
/ c+ U8 I1 m7 |) H3 [/ D
" c+ L, h* R$ B; i+ Q |
|