Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added commands and variables to run something when client connect/disconnect #169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/selkies_gstreamer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ def main():
parser.add_argument('--metrics_http_port',
default=os.environ.get('SELKIES_METRICS_HTTP_PORT', '8000'),
help='Port to start the Prometheus metrics server on')
parser.add_argument('--start-after-connect',
default=os.environ.get('SELKIES_START_AFTER_CONNECT', ''),
help='Program or command to run on server after the first client has connected')
parser.add_argument('--start-after-disconnect',
default=os.environ.get('SELKIES_START_AFTER_DISCONNECT', ''),
help='Program or command to run on server after the last client has disconnected')
parser.add_argument('--debug', action='store_true',
help='Enable debug logging')
args = parser.parse_args()
Expand Down Expand Up @@ -516,6 +522,12 @@ def main():
using_webrtc_csv = args.enable_webrtc_statistics.lower() == 'true'
metrics = Metrics(int(args.metrics_http_port), using_webrtc_csv)

# Setup commands to run on connect and disconnect
if args.start_after_connect != '':
os.environ['SELKIES_START_AFTER_CONNECT'] = args.start_after_connect
if args.start_after_disconnect != '':
os.environ['SELKIES_START_AFTER_DISCONNECT'] = args.start_after_disconnect

# Initialize the signalling client
using_https = args.enable_https.lower() == 'true'
using_basic_auth = args.enable_basic_auth.lower() == 'true'
Expand Down
20 changes: 19 additions & 1 deletion src/selkies_gstreamer/signalling_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# Author: Nirbheek Chauhan <[email protected]>

import os
import base64
import sys
import ssl
import logging
Expand All @@ -32,6 +31,7 @@
import hashlib
import hmac
import base64
import subprocess

from pathlib import Path
from http import HTTPStatus
Expand Down Expand Up @@ -277,6 +277,15 @@ async def cleanup_session(self, uid):
wso, oaddr, _, _ = self.peers[other_id]
del self.peers[other_id]
await wso.close()
# Run application after last session disconnected
try:
if 'SELKIES_START_AFTER_DISCONNECT' in os.environ and len(self.sessions) == 0:
if os.environ['SELKIES_START_AFTER_DISCONNECT'] != '':
command = os.environ['SELKIES_START_AFTER_DISCONNECT'].split(' ')
subprocess.Popen(command, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except Exception as e:
logger.error('Failed to run SELKIES_START_AFTER_DISCONNECT: {}'.format(e))

async def cleanup_room(self, uid, room_id):
room_peers = self.rooms[room_id]
Expand Down Expand Up @@ -367,6 +376,15 @@ async def connection_handler(self, ws, uid, meta=None):
wsc = self.peers[callee_id][0]
logger.info('Session from {!r} ({!r}) to {!r} ({!r})'
''.format(uid, raddr, callee_id, wsc.remote_address))
# Run application on first session connection
try:
if 'SELKIES_START_AFTER_CONNECT' in os.environ and len(self.sessions) == 0:
if os.environ['SELKIES_START_AFTER_CONNECT'] != '':
command = os.environ['SELKIES_START_AFTER_CONNECT'].split(' ')
subprocess.Popen(command, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except Exception as e:
logger.error('Failed to run SELKIES_START_AFTER_CONNECT: {}'.format(e))
# Register session
self.peers[uid][2] = peer_status = 'session'
self.sessions[uid] = callee_id
Expand Down