Skip to content

Commit

Permalink
feat(alert): add option to use custom socket
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Aug 19, 2023
1 parent a7d29a3 commit 5245db8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions plexapi/alert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import json
import socket
from typing import Callable
import threading

from plexapi import log
Expand Down Expand Up @@ -32,15 +34,17 @@ class AlertListener(threading.Thread):
callbackError (func): Callback function to call on errors. The callback function
will be sent a single argument 'error' which will contain the Error object.
:samp:`def my_callback(error): ...`
ws_socket (socket): Socket to use for the connection. If not specified, a new socket will be created.
"""
key = '/:/websockets/notifications'

def __init__(self, server, callback=None, callbackError=None):
def __init__(self, server, callback: Callable = None, callbackError: Callable = None, ws_socket: socket = None):
super(AlertListener, self).__init__()
self.daemon = True
self._server = server
self._callback = callback
self._callbackError = callbackError
self._socket = ws_socket
self._ws = None

def run(self):
Expand All @@ -52,8 +56,13 @@ def run(self):
# create the websocket connection
url = self._server.url(self.key, includeToken=True).replace('http', 'ws')
log.info('Starting AlertListener: %s', url)
self._ws = websocket.WebSocketApp(url, on_message=self._onMessage,
on_error=self._onError)

if self._socket:
self._ws = websocket.create_connection(url=url, on_message=self._onMessage, on_error=self._onError,
socket=self._socket)
else:
self._ws = websocket.WebSocketApp(url, on_message=self._onMessage, on_error=self._onError)

self._ws.run_forever()

def stop(self):
Expand Down

0 comments on commit 5245db8

Please sign in to comment.