管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
主要使用 webstocket" \+ `9 U: p3 I+ O& w
上代码 前端 观看页面 - <script type="text/javascript"> o+ p2 {2 Q6 j8 d1 Y. O; e% _
- var ws = new WebSocket('ws://192.168.0.150:9502');
5 F0 V( W! z+ h% I1 u- }; I
. P& f* g( w9 Y2 J8 q1 J( f- ws.onopen = function() {& h' z# i$ m& X5 s0 |- x; ?, i
- console.log("连接成功"); F& c7 a% Z" H
- };
. h! I/ t- Z ^7 ], E. }; z; ]2 N3 v - ws.onmessage = function(e) {
+ S' D8 w) {, Z' A' u - console.log(e);$ E7 E" K$ X: ~/ Q- m0 u- Z- {, x
- + A8 B( w; y ~0 G. M- s4 T" g, ^
- var data = e.data;' l t% K% B# B @ f s; D
- document.getElementById('player').src=data;
7 ?5 Z$ S4 p2 Y- S0 K - };. k( k5 c3 S# z
- ws.onerror = function() {
0 H! Y2 e \1 t3 h V( K: _ i - console.log("关闭连接");. i5 {9 h" ^& P* l; ]$ O
- };
, ^, Y) o# ?2 M( n7 b9 u - </script>
复制代码录像页面 5 K2 f% g! g- Z3 @6 R$ Q
最好用火狐测试。 - <canvas id="output" style="display:none"></canvas>+ O% q- B* t& ~) E! j1 X) P
1 r# s [4 j. e8 P8 H3 ^
; @+ ?, w+ a1 b/ V1 W [3 F
4 \+ r! P2 a5 e2 V# `# O- <script type="text/javascript" charset="utf-8">$ |! h B m. J: V# D& v" q
- 6 ~( J" \/ |6 a9 z+ N* {7 A
-
7 N( Y8 A4 C5 y - var socket = new WebSocket('ws://192.168.0.150:9502'); A& G# E8 ]7 @ k+ v, B, w/ {( j
- //socket.send("嗨我登陆了");7 @% }$ ]8 r$ ?1 w! ~& D
- 8 u2 Z/ N T3 K- c4 ^
- var back = document.getElementById('output');
9 D7 z$ M( b, Z- z - var backcontext = back.getContext('2d');8 C Z! ~1 U, u3 k3 X
- var video = document.getElementsByTagName('video')[0];7 z/ o2 X _: h; m
-
5 l, K2 }) K4 Z- x; Y9 G$ v - var success = function(stream){
. j! \( o* {$ l5 u6 U5 R6 m - video.src = window.URL.createObjectURL(stream);6 s% C# @6 Y, b0 |1 k( \* j
- } i3 ?1 C+ |2 B$ Y F
- " H4 F3 ?& |" y5 N, P6 n' O
- socket.onopen = function(){9 p, A7 Y/ \% [
- draw();
! S$ f w; K, Z' D. b - }# K8 b( W( l: C Q5 m0 B" H* c( E- ^
- 7 G- u' A: u" R6 a# E0 G5 n/ e
- var draw = function(){1 ~" r4 b r. b M" x6 m" p# g
- try{* i( a! u5 K8 I/ l
- backcontext.drawImage(video,0,0, back.width, back.height);
( a; l o* E* a4 f1 c - }catch(e){- b/ o8 w' n3 p/ K: `0 l" ^$ j' [
- if (e.name == "NS_ERROR_NOT_AVAILABLE") {2 Z. u5 K1 r8 G2 y" q/ w
- return setTimeout(draw, 100);" l) _0 [* b# [- x. c/ j
- } else {! t9 ?% |" Z- Y3 |" X% \2 H
- throw e;
, T& n$ F& S( d6 ~8 a( T - }+ ^7 L( \' X/ z( N: ^; T
- }/ v- L$ f0 T1 \ r" J0 V& X# A
- if(video.src){/ @% O/ p% B0 @9 ~8 D( ?) I
- socket.send(back.toDataURL("image/jpeg", 0.5));
4 A, W9 w, Q5 g/ H - }! ]2 X& B8 r+ X C/ r! I. R+ p. V+ e
- setTimeout(draw, 100);8 Q' F$ m# _) t8 i7 |4 E, @: l1 C3 N
- }- i6 k* z1 G0 S' ^; ~# w9 I
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
7 H& M. R3 t* _# C - navigator.mozGetUserMedia || navigator.msGetUserMedia;
6 g- B4 d/ n5 J! L. |* M+ B - navigator.getUserMedia({video:true, audio:false}, success, console.log);
4 W8 c5 {9 x! W/ r - </script>
复制代码- php
! O* o( |' a) y# x/ b - % ]2 h" t+ m. l( p! ^8 x# O+ j) l
- $server = new swoole_websocket_server("0.0.0.0", 9502);8 `9 r* K+ J- n' d x/ Y
- / x2 [1 ^1 D! S+ r: W! e8 Y
- $server->on('open', function (swoole_websocket_server $server, $request) {
+ x1 c$ ]/ l7 X) x% N7 B5 f. T: W" d
! \) L. [5 i l4 k6 U9 R7 `$ v- echo "新用户id:{$request->fd}加入了\n";
( u% F# Q t7 n3 d9 T3 { - // echo "server: handshake success with fd{$request->fd}\n";1 }+ R$ E; o7 {9 `, \
- });
9 R" U: N! o" S! @2 h8 i
8 K" V4 ]2 a3 s3 ^2 [- $server->on('message', function (swoole_websocket_server $server, $frame) {
: v) ^. u) o1 S8 B/ d
7 f% C ?+ o* J; ~4 F- M+ g' n- //循环所有数据1 F5 {, l8 A* ^! T: j/ N
- foreach($server->connections as $fd) {. l; m$ A Z `
- //返回数据
" z! J g4 L3 H7 n/ M5 i! o% ~. f - $server->push($fd, $frame->data); F/ y b8 l" g) L. }' V
- }
; X- f8 B+ a1 v Z$ H
: G/ m" @, f: P- });( P7 {- j k9 z2 m8 E9 L2 E3 n
- 4 x7 T7 V! R5 k& `# ~4 b
- $server->on('close', function ($ser, $fd) {9 J0 `6 B: X' O. N7 H2 Q& g. `
- echo "用户id: {$fd} 退出\n";
5 J8 ~4 Y2 Y6 h - });
/ h Q0 H& O" g- T7 Y8 E
2 Q6 A, T: I7 o: R- $server->start();
复制代码 " B" @+ _* i0 a6 f' u+ [
0 H9 L9 {, w2 d B( q9 ]
$ d# j' H! |+ J6 f# M
+ _! J# S( b) b* L, `6 E5 S+ ]2 }) w8 O/ \, Q" l0 j4 _7 F3 f( Z
) p; k8 r3 j# J. o
" e+ v$ J- l$ {- o' E
( Q" M% B) _# W8 {
/ L& c* ]; y# e+ c, D; I# W# Z
! K! M6 c |8 y A( Z
- D/ C( p9 B& h" Z2 ?$ E2 o8 F) s U4 i# E! [, P& |/ S7 X
|
|