cncml手绘网
标题: swoole 直播 [打印本页]
作者: admin 时间: 2020-3-31 19:41
标题: swoole 直播
主要使用 webstocket
& \+ g2 ]* ]. H" Z& C) O1 c5 R上代码
前端
观看页面
- <script type="text/javascript">
% {- a" s7 y2 I4 O+ [ - var ws = new WebSocket('ws://192.168.0.150:9502');
5 v3 t2 x/ H. J/ {! N- d" C! V - 6 n% |# }1 e! H4 n3 F6 F
- ws.onopen = function() {
# p5 `9 Z$ j7 r2 a3 c- A - console.log("连接成功");9 W, \8 B% S# X# ^
- };
}4 L1 ]$ [/ j r - ws.onmessage = function(e) {
2 j; x4 h& ?4 J3 G6 }- x' G. F - console.log(e);
& H2 y6 ^1 D7 V8 C. \. B! x
6 Z3 D5 [; S% o9 h( ~4 B8 I% ~- var data = e.data;$ n3 g, p; W+ Q+ k
- document.getElementById('player').src=data;1 M* n0 q% W- |( R: k
- };) |$ X# d7 v/ J) V& O
- ws.onerror = function() {
: y9 I; ?- Q2 T - console.log("关闭连接");& U, O" r2 {, h. n, }. {
- };
+ W' A0 E7 c5 n0 f6 D5 I( c: A - </script>
复制代码录像页面
. l$ z8 z% ~8 J$ t最好用火狐测试。
- <canvas id="output" style="display:none"></canvas>
* C& ^0 x c5 L - ! _' X$ J$ \% W9 r* i$ Y
: p$ A- f% _, d9 _' p* x( P- 3 V" N: y0 T P* B1 G, _
- <script type="text/javascript" charset="utf-8">
0 U) m, U* b9 ^, b( v! d
4 Z' N+ U1 J' z- M% V0 z- 6 g7 I7 j2 U$ q% j( M
- var socket = new WebSocket('ws://192.168.0.150:9502');! H6 e1 k& M: g: Z e2 A8 H& J, l
- //socket.send("嗨我登陆了");
/ p& }9 H# W2 b6 c$ H6 U% ^
+ g( [& M- w$ f% c$ B- var back = document.getElementById('output');" z3 U, O: [& } S0 h
- var backcontext = back.getContext('2d');
3 V; m# [2 ~1 K( i - var video = document.getElementsByTagName('video')[0];
3 w1 H, d* Q7 R6 ~' A7 C - 0 D/ B& t- x: h; F4 [* {% K" q, F
- var success = function(stream){
[" N+ Q _9 Z& J# `* }- T - video.src = window.URL.createObjectURL(stream);9 v2 D& l7 R0 B1 o4 G
- }
" o" A3 r% R: ` - 1 S& V1 g* ?: c- l- Y6 w
- socket.onopen = function(){" J' u4 Q0 Q4 `
- draw();
3 e5 |9 w" x) ^ g9 v, T - }
6 Z4 B' }: L) ]* ^$ p; \4 U - 9 N. u; X) }; W# {4 D
- var draw = function(){1 Z( J3 U* q8 P- X. h) d
- try{1 r4 G% G/ ~$ t$ f5 J: O9 n+ J
- backcontext.drawImage(video,0,0, back.width, back.height);
) R: K4 h: Q; \ - }catch(e){
% B C" F. |+ W+ Y - if (e.name == "NS_ERROR_NOT_AVAILABLE") {
, H g4 ?2 k6 A0 J% m' ]' J( h - return setTimeout(draw, 100);
- a+ w) j0 w4 p0 d% n& a - } else {
0 N! R, ?! Q8 ? - throw e;+ k3 N8 S+ ^9 E R: O# W8 j
- }) g6 D% c+ {4 X+ d0 h* `& ~
- }
3 v! A3 O9 p, K - if(video.src){
% F: P& R [# T' p1 }1 I. x - socket.send(back.toDataURL("image/jpeg", 0.5));+ e+ n; H' o0 {4 a, h0 h. b) C
- }
* K1 W- }2 g0 i! z9 d: d, Z: h - setTimeout(draw, 100);& f2 E* \8 }# S; h. _7 O
- }& r' P. I/ `0 s0 \) }
- navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||* o) s) W l6 z: u8 P4 o+ K. ]
- navigator.mozGetUserMedia || navigator.msGetUserMedia;6 }& L! T; a6 u) O2 `. A
- navigator.getUserMedia({video:true, audio:false}, success, console.log);
) t9 O: ^- ~6 Q0 I1 N/ g4 N5 } - </script>
复制代码- php
. y' a4 f* d: G3 D - + l3 X; {# G1 L5 {: u$ W
- $server = new swoole_websocket_server("0.0.0.0", 9502);
. c: i0 U1 B- O, e; |! X
( f+ k: @2 ^! U: Q( S6 s- $server->on('open', function (swoole_websocket_server $server, $request) {
& |& E1 r N5 q x( q* W" r f/ W3 F" P - : Y' N' i. U0 y. F Y( i
- echo "新用户id:{$request->fd}加入了\n";( |5 \5 _% L% ]- d9 {; B( @
- // echo "server: handshake success with fd{$request->fd}\n";1 z4 ~; @: O2 d2 v
- });" c: H5 l# v' z4 z* u" M' a) @7 ~, l# e
& u* s9 v+ H$ q$ |- $server->on('message', function (swoole_websocket_server $server, $frame) {, W+ r7 _) L O/ U0 F/ z7 Z( u. U
5 i* o" O5 B9 r. i$ `9 B) L- //循环所有数据& G6 L& }9 M5 F; P5 U( D- Y
- foreach($server->connections as $fd) {
M: W f6 W$ |$ {+ b w - //返回数据$ Y. v3 U( H1 p' H
- $server->push($fd, $frame->data);- r' @9 @$ {' U0 Y/ v& `
- }" x) T, S6 x4 R3 S" A
}" k* _4 m, } m- });: p8 u6 k" C* F6 r% X) I( I7 {, ~( j
4 H4 P) q! K' w$ {; ~9 N, Q- $server->on('close', function ($ser, $fd) {
; L1 j# A) C4 v( Z9 a$ { - echo "用户id: {$fd} 退出\n";
, r2 {& J1 S2 {/ h5 a% a - });
: E* H: e3 t2 M( B! y - 9 u8 p. B: C% N1 Z0 {
- $server->start();
复制代码 - k% u- ~2 e% v$ l( J4 f9 p
7 |0 k+ L; r8 z1 }" M
% |) t, M: b) g
' B& R7 m, }( Q' t# ]7 _6 }2 X" y9 }/ z3 |9 Y0 ? i
; ?% I8 ]3 b+ X" |$ \
0 @! ~7 f/ V R; f \3 r5 L8 w" F r
8 ]7 ~; E7 c& {" {: U
7 X: x+ f1 W( X- y, Z* ]+ ^* v: [" F4 [4 ~
7 G: E8 G/ w3 s( \- d( O! T8 E0 X& d" A5 a3 t: f7 t) R
欢迎光临 cncml手绘网 (http://cncml.com/) |
Powered by Discuz! X3.2 |