管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
1 T. P: [2 t' [" h* f6 Z/ g9 s上代码 前端 观看页面 - <script type="text/javascript">; E6 ?! o' S& p1 A
- var ws = new WebSocket('ws://192.168.0.150:9502');
3 h5 v" c4 }4 L. ?4 J3 Y( S: p8 K - ! t6 g, f- p' y, w* W
- ws.onopen = function() {7 O5 ~' ^6 Z# Y' ~5 y
- console.log("连接成功");9 ~5 W: p1 r8 c
- };# K6 s' p6 }/ e
- ws.onmessage = function(e) {
: d+ a j4 T' X \ - console.log(e);1 @" M1 E t+ O
- ) R+ w6 U" g, w+ Y$ p8 m0 G
- var data = e.data;& K; N, D5 O# l0 _ d
- document.getElementById('player').src=data;
( [( T! }( b0 E4 c; c - };* r {; L) A0 Y4 H7 R. M
- ws.onerror = function() {* @- V. j3 k @- k
- console.log("关闭连接");
% H' V' `' u! D, I - };. u2 \3 g! P/ o0 h3 D
- </script>
复制代码录像页面
# B$ B- M, \$ G# E* r5 X最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>
0 A3 x1 C+ O7 N - ( }$ t8 U+ h2 |! q) ~7 s. H5 ^
: O: d5 b3 g* [+ n' H
6 T2 T9 f0 T _8 l9 ?% @8 S- <script type="text/javascript" charset="utf-8">- Q/ C# M- F% e7 K: u
- 5 b$ o' [5 [5 f. B5 k
- 8 x1 q5 H3 f$ A7 ]& g
- var socket = new WebSocket('ws://192.168.0.150:9502');
: \% Y; k7 `7 j" [+ R - //socket.send("嗨我登陆了");) ]' V; z" H& X5 f# u; @( J; W! [
+ ?! t: }8 Z* @3 _1 m- var back = document.getElementById('output');
. e" C; m( L! Z! w! s( |% x - var backcontext = back.getContext('2d');
: k0 ~( w! y- h6 Z: t7 T& | - var video = document.getElementsByTagName('video')[0];
" `; L) L& Z* E - 8 @' o: b3 u/ w1 z
- var success = function(stream){
9 z% P6 q& y0 x7 [- ? - video.src = window.URL.createObjectURL(stream);4 V, w7 |/ X1 a, A4 \
- }
% [( K; Y+ J+ m$ v; s' w/ D) t - ) i3 b( K1 s) U( M
- socket.onopen = function(){. @: |7 ~, U0 l
- draw();
! I" F# l9 V! f$ Y" y3 A1 D - }
2 ^/ r& m# i7 O/ D - * q5 x% w. L* d) U6 n1 ~" a q
- var draw = function(){; T3 P; B4 U2 u- t
- try{
7 M: _# O6 e$ G/ f1 B( H - backcontext.drawImage(video,0,0, back.width, back.height);0 O( q$ f8 \, I# r) A9 M6 V
- }catch(e){2 N, O+ l% q c5 I( p8 c+ L( T
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {
- o. D$ g. C4 G1 A - return setTimeout(draw, 100);! {% Q4 B! q8 O4 O g
- } else {7 y9 s( D$ K; h8 ^
- throw e;
; i) W" J) m; Q: b' r - }) V2 T4 }0 V. W I# J2 n
- }. N. F2 {. r. m2 [
- if(video.src){
1 g9 v! p9 ], R" k3 | - socket.send(back.toDataURL("image/jpeg", 0.5));
# p- a5 H2 E0 F3 O4 X& q - }
% u6 s5 \6 r Y) Q! s - setTimeout(draw, 100);% H9 j6 O$ y- V. @2 N' O5 Q
- }( x9 {$ V4 U5 C. o
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
8 E9 h3 @+ j4 h2 f4 }8 l - navigator.mozGetUserMedia || navigator.msGetUserMedia;: A. l# J `0 | s
- navigator.getUserMedia({video:true, audio:false}, success, console.log);, x& X$ [% e; \
- </script>
复制代码- php" x+ s6 x7 A/ q9 D
- 5 w* T; o3 g( i; ]8 [/ A
- $server = new swoole_websocket_server("0.0.0.0", 9502);" d3 K9 d! R( L4 b) z1 @# F
- - r5 B/ z- K! X F
- $server->on('open', function (swoole_websocket_server $server, $request) {3 {. q# S% U& |2 \/ U
- 0 ~+ z" i" R9 B% X4 r
- echo "新用户id:{$request->fd}加入了\n"; h# l) L1 R: T2 J. W& y/ b9 n
- // echo "server: handshake success with fd{$request->fd}\n";
: E, ^; p5 h9 f% C2 ? - });/ j0 a# }* J( x; A, L
- # U# V, ]6 A: ^; E: R/ O7 X
- $server->on('message', function (swoole_websocket_server $server, $frame) {
, ?& G' m- v6 f5 x" C8 [ - ) e% M# @* R' s& Z. ]' m# g
- //循环所有数据
. Q9 m7 M, O/ m - foreach($server->connections as $fd) {4 e' |, C3 f7 U, s5 u
- //返回数据& e2 V# v; a$ K2 @0 Q7 O( u
- $server->push($fd, $frame->data);
2 K3 T* h, U; x; H; O' S - }3 @2 R' V) u' g3 g+ E
1 @" s+ ]: J. P* s7 a' I- });- G/ }2 N6 ]" r$ `9 N3 P
- " n- p: R! J( C! L
- $server->on('close', function ($ser, $fd) {& G' O2 p7 E6 {
- echo "用户id: {$fd} 退出\n";
0 y! c+ s( P# j: e T - });
8 C3 ^1 O$ r* A4 e& r - # d, H, I" Z# t; |5 {
- $server->start();
复制代码 + P& ~1 t9 o+ {& K7 J, Q
" M0 N5 o2 _7 V' I+ V, K! m
1 s: a+ C, S$ x. g7 j3 C
( q) ~& B( ?& j2 }1 @' U$ {7 G
$ @8 J, e0 x7 K, J
5 i) E! }; R$ J1 \( z" o
$ d, j5 x g+ n4 n* p
& i4 J% g$ D; T/ @# N+ {, s5 w# d3 N$ a/ e# T
' \1 |" l6 l9 k2 B" p5 E
. f! c% O8 V3 ]" X0 _
- P+ Z* p3 e6 E+ Y |
|