管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket j1 ^- o+ J- e$ k8 M) J
上代码 前端 观看页面 - <script type="text/javascript">. _) S3 q3 g) l8 r' n
- var ws = new WebSocket('ws://192.168.0.150:9502');3 X3 }3 F# r4 y/ g
- ( A: r3 B+ Z$ h: A8 \
- ws.onopen = function() {
0 ~, ]$ t+ D' k9 |& E; v% G - console.log("连接成功");
y& J8 }, w# C. A - };( O. J& i1 Z7 r
- ws.onmessage = function(e) {
# ^, R% C3 X0 x" H# s% g - console.log(e);
8 e8 S/ o+ v" _
- ?4 b0 p+ F; o- var data = e.data;
2 ~, F( s( `6 C. e6 _ - document.getElementById('player').src=data;' a$ Z5 Y( O+ j
- };
+ C P+ R! ]7 E; e - ws.onerror = function() {
u# @- Q2 ^) R% ]6 P7 b$ Z - console.log("关闭连接");
) ?; d8 t, j6 J! j% g* w0 a - };
9 S4 T1 r0 _; Z; a" ~' j - </script>
复制代码录像页面
% {3 X( j6 ^% b最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>4 R; I( | `7 [. i2 s8 I) w
! z2 G8 ] q, ?8 N, T+ k5 \2 H- + {2 B' d4 o) u& v b; S j, D( |, @
- . R. ` L$ U& h. q! c
- <script type="text/javascript" charset="utf-8">
* B* T- e' L* I7 e) o* e
2 |% r P- n8 m# e2 T-
7 t9 v3 U, G. @$ \ - var socket = new WebSocket('ws://192.168.0.150:9502');0 X' u% w* E1 i6 F; C
- //socket.send("嗨我登陆了");: j( j. X9 y! ^7 p
, s, B' g0 C& A5 ~- var back = document.getElementById('output');# O2 V4 U( K, n) _
- var backcontext = back.getContext('2d');0 q8 v3 Z# l% H# [5 s- V
- var video = document.getElementsByTagName('video')[0];) _6 M3 k+ A8 I8 N3 w" l% m1 ?
-
4 H" h# g5 x/ n% y6 e - var success = function(stream){% A6 o/ m# w$ D% h1 n% T
- video.src = window.URL.createObjectURL(stream);, C0 b( W' u, m; S6 R( w. t
- }
# Q5 b) @/ b4 H+ v6 b
3 n9 X2 k8 a$ q5 p; D- socket.onopen = function(){) g x+ L* a3 Y0 r E6 w
- draw();0 |" u# N3 ~6 W2 Q1 _# z
- }8 [. g( B! f* z/ m
u; Y) ~, e! q- j# \- var draw = function(){2 v" q3 E, H% e. d# ?
- try{
- b& ]6 W4 |; j1 _6 P8 j - backcontext.drawImage(video,0,0, back.width, back.height);% y2 K& u" G% U4 }
- }catch(e){
* J) F# l& A0 E* Z5 e - if (e.name == "NS_ERROR_NOT_AVAILABLE") {: w7 O1 g% u# Z! ~+ m
- return setTimeout(draw, 100);
! V: G( C, R. u7 ~5 O - } else { P2 \; Z* x& I7 u
- throw e;
* S9 r8 ]$ L2 j4 f+ B5 l1 p% { - }
9 Y5 z( c+ F" a. `0 K1 O6 l" d - }) `1 k4 ]: l5 k1 h. a
- if(video.src){* Z+ k/ ~8 q2 D, J1 P; w' I
- socket.send(back.toDataURL("image/jpeg", 0.5));5 N5 J: r4 g3 h7 S
- }
+ A( Z/ z. |; ] - setTimeout(draw, 100);
& W$ j6 @+ l4 }* u& A/ H2 Y - }) a6 f) R9 D, Q; U4 y' _! x9 R
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||: d# `. }# j( q% s
- navigator.mozGetUserMedia || navigator.msGetUserMedia;
0 j9 M6 C% |: f3 V: b - navigator.getUserMedia({video:true, audio:false}, success, console.log);( |6 e* ~" `; Y0 Z
- </script>
复制代码- php
; c5 r- h. t. \( t$ `
7 M8 D5 R1 U& W0 }. _ P5 Y- $server = new swoole_websocket_server("0.0.0.0", 9502);# `; C7 I9 p( [ K$ J/ C6 o
% s! |8 k& K; H+ n9 g& B3 s/ n0 k- $server->on('open', function (swoole_websocket_server $server, $request) {
, o9 M8 J' g; }5 f( S b
! r! O! t3 a. P* u. p* M- echo "新用户id:{$request->fd}加入了\n";
' y2 C# G* ], L' P' D k - // echo "server: handshake success with fd{$request->fd}\n";" j! D. ]9 f1 u
- });
. ]/ K2 L8 |3 ` [! Y
, r) o( m! d+ S* E( u, U# u- $server->on('message', function (swoole_websocket_server $server, $frame) {* w5 f& h. I' I* i" e2 q
/ y1 z6 p+ M1 J8 n- //循环所有数据# L) l. X7 O* E4 k7 b
- foreach($server->connections as $fd) {
: D6 V$ {1 F2 T( Y4 [ - //返回数据
( F# c( _8 G' E! m3 n; w8 z - $server->push($fd, $frame->data);2 s6 U4 y& u9 }6 X
- } h8 g; J0 ~) z% J* [, K2 b
) D. M) M, }& F- Z/ J* W- });. G4 H" F8 m- f/ ^& a7 G
, u+ ^' n" C% `- $server->on('close', function ($ser, $fd) {
H* N7 Z8 C- _0 {) T p - echo "用户id: {$fd} 退出\n";
3 W# |2 |. W3 e0 v# Z6 C - });5 J! v- Q9 [5 f0 d* ]
+ [! h8 {+ k6 [7 O0 n- $server->start();
复制代码
( f% I. s; K+ c" Y
$ n' b# ]# O% D9 \
: N6 ?, j/ O3 _; @- b- O; `
$ Z2 H4 g! O( w4 U, p5 V; N9 M2 r( p* n# ~: {% o) M
1 T8 v, j% p" X7 q. @
( P6 e3 w/ _6 f( [ P% I# ]
- [( b7 n8 N3 H9 u( t: ~( T" t y7 T% l& ?) E7 ?8 n) P/ @
3 x5 F. M" N% W3 i q# P& @ @( ?
) Z, O' a& @. l/ v4 W6 i
|
|