You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importaxiosfrom'axios';constapi=axios.create({baseURL: process.env.VUE_APP_API_URL||'/api',timeout: 10000,headers: {'Content-Type': 'application/json','Accept': 'application/json'}});// Add a request interceptor for authenticationapi.interceptors.request.use(config=>{consttoken=localStorage.getItem('auth_token');if(token){config.headers['Authorization']=`Bearer ${token}`;}returnconfig;},error=>{returnPromise.reject(error);});// Add a response interceptor for error handlingapi.interceptors.response.use(response=>response,error=>{if(error.response&&error.response.status===401){// Handle unauthorized accesslocalStorage.removeItem('auth_token');window.location.href='/login';}returnPromise.reject(error);});exportdefault{// Channel endpointsgetChannels(){returnapi.get('/channels');},getChannel(id){returnapi.get(`/channels/${id}`);},getStreamUrl(id){returnapi.get(`/channels/${id}/stream-url`);},// Stream management endpointsstartStream(id){returnapi.post(`/streams/${id}/start`);},stopStream(id){returnapi.post(`/streams/${id}/stop`);},// Monitoring endpointsgetStreamStatus(id){returnapi.get(`/monitoring/${id}/status`);},getAllStreamStatuses(){returnapi.get('/monitoring/statuses');}};
channelService.js
importapifrom'./api';exportdefault{/** * Fetch all channels with their status * @returns {Promise} */asyncfetchChannels(){try{constresponse=awaitapi.getChannels();returnresponse.data.data;}catch(error){console.error('Error fetching channels:',error);throwerror;}},/** * Fetch a single channel with detailed information * @param {number} id - Channel ID * @returns {Promise} */asyncfetchChannel(id){try{constresponse=awaitapi.getChannel(id);returnresponse.data.data;}catch(error){console.error(`Error fetching channel ${id}:`,error);throwerror;}},/** * Get the HLS stream URL for a channel * @param {number} id - Channel ID * @returns {Promise<string>} */asyncgetStreamUrl(id){try{constresponse=awaitapi.getStreamUrl(id);returnresponse.data.stream_url;}catch(error){console.error(`Error getting stream URL for channel ${id}:`,error);throwerror;}},/** * Start streaming a channel * @param {number} id - Channel ID * @returns {Promise} */asyncstartStream(id){try{constresponse=awaitapi.startStream(id);returnresponse.data;}catch(error){console.error(`Error starting stream for channel ${id}:`,error);throwerror;}},/** * Stop streaming a channel * @param {number} id - Channel ID * @returns {Promise} */asyncstopStream(id){try{constresponse=awaitapi.stopStream(id);returnresponse.data;}catch(error){console.error(`Error stopping stream for channel ${id}:`,error);throwerror;}}};
4. Laravel WebSocket Broadcasting
StreamStatusChanged.php (Event)
<?phpnamespaceApp\Events;
useApp\Models\Channel;
useApp\Models\StreamStatus;
useIlluminate\Broadcasting\ChannelasBroadcastChannel;
useIlluminate\Broadcasting\InteractsWithSockets;
useIlluminate\Broadcasting\PresenceChannel;
useIlluminate\Broadcasting\PrivateChannel;
useIlluminate\Contracts\Broadcasting\ShouldBroadcast;
useIlluminate\Foundation\Events\Dispatchable;
useIlluminate\Queue\SerializesModels;
class StreamStatusChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public$channel;
public$status;
/** * Create a new event instance. * * @param Channel $channel * @param StreamStatus $status */publicfunction__construct(Channel$channel, StreamStatus$status)
{
$this->channel = $channel;
$this->status = $status;
}
/** * Get the channels the event should broadcast on. * * @return BroadcastChannel|array */publicfunctionbroadcastOn(): BroadcastChannel|array
{
returnnewBroadcastChannel('stream-status');
}
/** * Get the data to broadcast. * * @return array */publicfunctionbroadcastWith(): array
{
return [
'channel_id' => $this->channel->id,
'name' => $this->channel->name,
'status' => $this->status->status,
'is_online' => $this->status->is_online,
'last_online' => $this->status->last_online,
'bitrate' => $this->status->bitrate,
'timestamp' => now()->toIso8601String()
];
}
/** * The event's broadcast name. * * @return string */publicfunctionbroadcastAs(): string
{
return'StreamStatusChanged';
}
}
channels.php (Broadcasting Routes)
<?phpuseIlluminate\Support\Facades\Broadcast;
/*|--------------------------------------------------------------------------| Broadcast Channels|--------------------------------------------------------------------------|| Here you may register all of the event broadcasting channels that your| application supports. The given channel authorization callbacks are| used to check if an authenticated user can listen to the channel.|*/
Broadcast::channel('stream-status', function ($user) {
returntrue; // Public channel, anyone can listen
});
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});