管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket
* N9 y: I0 M/ t2 L$ m$ H上代码 前端 观看页面 - <script type="text/javascript">
. t- \$ g' F: u$ y8 p - var ws = new WebSocket('ws://192.168.0.150:9502');+ x! ?1 k! w0 d k) _0 [
- 6 M& ?2 T4 i7 C6 ~/ r# f
- ws.onopen = function() {8 b, \7 g5 q9 N( Z
- console.log("连接成功");% v1 \6 e! I' f4 _5 [* t% D5 c2 _' d
- };
7 }7 |; e. y" g6 p) [; @% e - ws.onmessage = function(e) {2 B9 E7 w/ P) W" f/ W+ {
- console.log(e);4 _% z+ j' Y% q* `( k l
- % q- G |0 ?/ {* k5 T# P2 r' z2 y
- var data = e.data;
; [% C: [% P0 k- B f - document.getElementById('player').src=data;7 A7 i) p. ?6 {# M
- };5 t/ Y {5 p+ A
- ws.onerror = function() {
+ @% P6 H7 q- b2 p3 a( Y! m - console.log("关闭连接");
B) {" t3 e. z) p - };
& N; J6 i" O% g& x5 j1 L* |5 Z! k5 Y - </script>
复制代码录像页面 ! D! [( u8 h: W/ N" K9 P
最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>1 u2 o. B Y& `0 o. f. c' }7 ^+ J7 d
& g- b4 X- s. @( q- 1 ~1 I8 d! c: J
- 8 e. o% S6 T$ ?3 `& s
- <script type="text/javascript" charset="utf-8">+ L# W7 C2 n" b6 F# b& x+ c4 u" ]6 o
- ( s$ ^/ Q5 M$ i1 w3 X
- ; f! `! L" |, D5 W1 s
- var socket = new WebSocket('ws://192.168.0.150:9502');" ]+ \8 J% F% w7 s
- //socket.send("嗨我登陆了");
) r& x# ?1 ?% A - ) p1 a8 p" S" O
- var back = document.getElementById('output');
3 j# L, M" z0 q$ Y$ L8 T5 ?! J - var backcontext = back.getContext('2d');: m9 Y4 S" ~9 }" y- u# t5 j
- var video = document.getElementsByTagName('video')[0];
7 B3 s3 U1 x8 P* G- F9 X4 S -
8 j7 e' I- }! Y - var success = function(stream){4 @% h* B) |& l6 P# ]$ n( g% v; h
- video.src = window.URL.createObjectURL(stream);, I, \% F: \/ t- ]; Y5 h/ G6 _) |
- }1 H+ H" M$ E, V( f0 k0 [0 y
- 0 ~8 p( W* k3 O: I9 V/ D- Z1 n
- socket.onopen = function(){
+ B1 n* ~6 M4 }# p+ o% ]8 A6 D( [4 J - draw();/ u% L& G8 E9 M& a
- }( Y8 b3 \) _0 Q' J0 k: g w
- * x8 a% ^; g2 A. ]2 s* o
- var draw = function(){% |. S; J# u# F) x" K8 N# c
- try{7 f$ \" E5 Z$ g$ k- |3 a$ @+ d
- backcontext.drawImage(video,0,0, back.width, back.height);
1 P! w0 d2 ~; Q - }catch(e){9 A, v% k2 k, {7 J5 K, ]" n% J. k
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {
7 d* e: R# ^3 q - return setTimeout(draw, 100);: u0 T5 r0 B9 ?, N- ^# u( z
- } else {' Q$ A+ B/ y5 m: c
- throw e;
, V5 R- p( C4 H; b z! Q# I - }
. x( G7 E b: [9 K& } - }7 Y# \5 s0 H3 q5 h
- if(video.src){
3 A: s* y% L! O. ~+ P! ?5 _ - socket.send(back.toDataURL("image/jpeg", 0.5));
; ?% \0 F- f# ]0 H8 V - }3 l( A ]6 ^5 p4 v4 V7 z7 s9 n
- setTimeout(draw, 100);, ]; @6 n2 l; e! A( D) g; l
- }7 K; c) |7 z6 y" D0 X
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
$ C. e8 ?: x1 s, z- r _ j - navigator.mozGetUserMedia || navigator.msGetUserMedia;7 M9 G2 J2 r1 ^# _6 D
- navigator.getUserMedia({video:true, audio:false}, success, console.log);: D2 v6 G, p c& }; @
- </script>
复制代码- php
- l. i, v+ {: F: w5 N% B, j, m1 m
, T3 w6 Y# B7 \7 j8 V- w- $server = new swoole_websocket_server("0.0.0.0", 9502);* W- u1 [7 b+ F, }1 U
) Q9 j% a5 `" s0 [9 X, k. [- $server->on('open', function (swoole_websocket_server $server, $request) {) j+ I$ t" {9 m4 D+ C- w6 w+ x
- 2 b/ B- r" h0 S/ g
- echo "新用户id:{$request->fd}加入了\n";3 {& x! ^3 F7 m) q' C! B' B- P0 v
- // echo "server: handshake success with fd{$request->fd}\n";
) E. |" W) }6 E5 J5 Y - });
1 s! c6 R# j2 k. y1 z, u - 1 E9 I/ J8 T$ L/ b3 F
- $server->on('message', function (swoole_websocket_server $server, $frame) { q* X7 q m" F* v" ]. x
# d' w3 r5 y# Z1 C& p% u5 G- //循环所有数据
: Q" K7 x D- T, Q* m - foreach($server->connections as $fd) {6 N8 i' F# a$ \9 w. r& B7 o
- //返回数据# C- @ Q# ^% f" ~
- $server->push($fd, $frame->data);. i" o, S7 M" V$ K' J4 _0 g
- }. r) f2 L) J0 g9 h
! O! G1 ?' D' z/ j, \5 T- });/ K! u5 e% P% G1 r
* R* F9 ]+ r( q- $server->on('close', function ($ser, $fd) {
, }, T0 n0 x) b - echo "用户id: {$fd} 退出\n";
9 S$ G# u3 ^2 g, e - });) R# X N% [/ o, X9 ?
+ {1 F: x! O% ?* h( V5 }1 X: R- $server->start();
复制代码
* }3 o" r j" q8 y2 w* B6 ]
7 u6 V; I2 J Z+ o6 J
# C$ Y. z4 p7 Z
w/ `# b0 ^2 V! i% j
& a; i! l& W; X) \* p: x+ u4 S4 T0 t- A5 c$ D
8 q2 s8 P# E: b3 g: _2 y6 k' ^: B. g9 v
& C' p5 R9 \ y& Q2 E0 A* v6 d! N8 V# ?( G$ C
1 ?6 H$ v2 m: }2 n! M0 q* h7 j9 l' b) `. p5 C+ A( W
|
|