Skip to content

Instantly share code, notes, and snippets.

@rmcauley
Created March 30, 2016 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmcauley/12b2018af6b5612e2a58e5734971c4b8 to your computer and use it in GitHub Desktop.
Save rmcauley/12b2018af6b5612e2a58e5734971c4b8 to your computer and use it in GitHub Desktop.
# this is for websocket support, takes a string message that is ostensibly JSON formatted from the browser
# formatting should be { "action": "[name of action/call/function]", "sid": [integer] }
# any other arguments will be handled by an individual function that takes care of validation
# except auth which you'll see below
try:
message = json.loads(message)
except:
self.write_message({ "wserror": { "tl_key": "invalid_json", "text": self.locale.translate("invalid_json") } })
return
if not message.action:
self.write_message({ "wserror": { "tl_key": "missing_argument", "text": self.locale.translate("missing_argument", argument="action") } })
return
if not message.sid:
self.write_message({ "wserror": { "tl_key": "missing_station_id", "text": self.locale.translate("missing_station_id") } })
return
if not message.sid in config.station_ids:
self.write_message({ "wserror": { "tl_key": "invalid_station_id", "text": self.locale.translate("invalid_station_id") } })
return
if not self.authorized and not message.action == "auth":
self.write_message({ "wserror": { "tl_key": "auth_required", "text": self.locale.translate("auth_required") } })
return
if message.action == "auth":
if not message.user_id:
self.write_message({ "wserror": { "tl_key": "missing_argument", "text": self.locale.translate("missing_argument", argument="user_id") } })
if not isinstance(message.user_id, numbers.Number):
self.write_message({ "wserror": { "tl_key": "invalid_argument", "text": self.locale.translate("invalid_argument", argument="user_id") } })
if not message.key:
self.write_message({ "wserror": { "tl_key": "missing_argument", "text": self.locale.translate("missing_argument", argument="key") } })
self._auth(message.sid, message.user_id, message.key)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment