管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket9 X" B1 S% a( G3 P9 G% o
上代码 前端 观看页面 - <script type="text/javascript">4 c) I) o0 H" K6 ^0 u) w
- var ws = new WebSocket('ws://192.168.0.150:9502');
, W) a. L- I' w3 ]/ \$ _( [ - ; M% r) J/ |# R1 T* V
- ws.onopen = function() {9 F2 k' y$ G0 V, X- L% l# V# [
- console.log("连接成功");
5 Q9 i( ^/ E& F C9 ~( I) `: Q; q - };! N1 w/ X' A5 o3 g2 O
- ws.onmessage = function(e) {8 s! o3 Q/ F% J1 j) x4 f3 x
- console.log(e);' Z/ Z4 i9 W( `! ~
- & o5 U3 \0 Y- E
- var data = e.data;; Y! M5 F& B. z9 I
- document.getElementById('player').src=data;
8 Z& O w5 d$ A0 ]- I8 ?% b* u - };* i5 O2 c# H, G: f# e4 a( y
- ws.onerror = function() {
& P. f7 z% V$ T - console.log("关闭连接");
, v' T) t5 a4 U/ { - };
0 [/ [0 L7 h* y7 V5 K - </script>
复制代码录像页面
2 u0 j/ Q( F7 t' E: n) E9 c8 T最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>5 O$ g% `' m2 g/ u1 z" V. i: {
- _/ J; q* W0 N9 S: }
A9 r# Z3 d4 s5 o- ' r1 c$ n2 S; W" a1 _) z0 b( t1 W. P4 ~
- <script type="text/javascript" charset="utf-8">
( G$ E$ }4 ^9 x1 K0 ~ I5 A# u - 1 D) {+ Q8 Y) X/ n1 E W, |
- 5 ~2 M9 I) [6 Q7 m8 H# I
- var socket = new WebSocket('ws://192.168.0.150:9502');
4 E5 Y+ n$ a1 I4 R8 ?" O - //socket.send("嗨我登陆了");
( H) e; c" |: e" J+ e - 3 w0 a$ Z. @! r/ m, a g
- var back = document.getElementById('output');1 ]# z! D) a5 P7 I9 Z
- var backcontext = back.getContext('2d');
! S0 m+ }0 X0 l% d4 j i - var video = document.getElementsByTagName('video')[0];$ n8 n. i+ Y; p E0 p& d
-
/ r) X( `% x% I8 |2 k4 H4 p; p7 Q/ G - var success = function(stream){
7 z c# c& j% l; N( ?9 [8 r: L T - video.src = window.URL.createObjectURL(stream);
$ s# [" V# Z, | - }/ \5 e: s) n9 \
$ |# D. |. T0 x! q) C2 w, ]9 o- socket.onopen = function(){
/ S" n" h$ S% x* o+ c' B - draw();
5 U/ K" Z; C6 m( X2 ?& Z - }7 l+ m4 @& `% S/ i$ v+ |. F/ j$ P, r
: b. r5 x: S& m* }& r' E- var draw = function(){- T; _5 x6 V9 m$ o8 D
- try{
1 [+ {+ u6 h- {: S6 S - backcontext.drawImage(video,0,0, back.width, back.height);! J5 O( q& P& v0 }6 R' O/ T$ N
- }catch(e){9 R. z3 O: M& F2 ` e8 X+ m
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {& R q- x+ N; U7 p/ P' F
- return setTimeout(draw, 100);
+ q) s+ H: d2 a; [/ _ - } else {
3 ^+ K* b! z4 c4 ` w$ S O9 \( m2 d8 {3 o - throw e;* g2 y. |* h9 W8 ~! g: Z
- }
) t4 i8 D+ ?) X8 ~' L& \8 D - }% z& G) L2 D) }3 S+ M! C9 p
- if(video.src){
6 w3 K5 m9 |; p; J9 \( t* Q# @ - socket.send(back.toDataURL("image/jpeg", 0.5));
2 \( g) ~2 w3 q: t8 t - }) i1 t! Q& W# Y8 s' y* L4 i
- setTimeout(draw, 100);. a4 P0 X1 G5 P% g0 q( H
- }' l) [" o* x& d& {, k5 ^
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
9 [% B4 o) |& v+ t - navigator.mozGetUserMedia || navigator.msGetUserMedia;
1 w+ [* S) u$ R - navigator.getUserMedia({video:true, audio:false}, success, console.log);
6 ?' l& X! x- ~7 z - </script>
复制代码- php
, ]" B! r# i, U, K+ y8 M
" C, a& u$ K8 \. E- $server = new swoole_websocket_server("0.0.0.0", 9502);
" O- T7 @" r O" K) x
) c; K, K% l3 z1 ~& M- $server->on('open', function (swoole_websocket_server $server, $request) {0 o& k' r' S, c/ N/ N, }; U3 _, K
- - R. W# h. T/ `2 t& D% ~
- echo "新用户id:{$request->fd}加入了\n";
% z' f* B7 x. W5 U; d) I - // echo "server: handshake success with fd{$request->fd}\n";
6 a1 J' l. Q7 l2 Q - });
( C8 m# {/ I$ W+ h7 S& R6 k - $ q! X& x* f3 l
- $server->on('message', function (swoole_websocket_server $server, $frame) {) k G3 \- E- M8 w; z
- / x. N8 S9 c+ d! [/ i
- //循环所有数据
+ U+ W# H5 B4 p( u - foreach($server->connections as $fd) {
. y* k+ x& D+ ^* a; T4 |# {* p9 E - //返回数据
$ H2 z' {! H- x8 y - $server->push($fd, $frame->data); l* k7 g7 z' u K
- }& l! ]- D7 G% m& Z, r
- " W! I }- J# ?8 H' o' [
- });
& f; S# u' a( E7 t) u6 E; J, O: Q- l
. G1 l* q! B% y- $server->on('close', function ($ser, $fd) {* N$ C% n6 V8 L$ X
- echo "用户id: {$fd} 退出\n";* @' a3 d& e6 V9 x
- });9 x( a6 N1 F* \2 E: k7 L
5 |9 H# t* X# W, G" J! j- $server->start();
复制代码 0 I, M8 z2 h" E, l/ q" P+ D$ G: s* D
- J8 h) s2 I& ]: Y+ |7 D
0 h/ O1 G2 I& q2 k# d& T \
( \+ n; i7 s7 ?- P0 N7 ]3 ^9 C9 l
7 s: h3 D# z; Y! o9 r9 R
- k- ?: p0 ?' |# ]& C& r
- B& v5 L$ n9 I2 B- _: G" {' v# m- T1 `3 g/ k! J$ }
& n# e- V7 x; `. }4 l& }; `( {& o$ `" C1 x3 v. A: v
9 G' \8 \% I; S5 D! G, Y# w
: u7 L% F9 F# n* y! } |
|