Login Page - Create Account

Support Board


Date/Time: Sun, 24 Nov 2024 20:54:44 +0000



Post From: [SC Relay Server] Documentation Needed

[2024-05-03 14:51:07]
MichaelPPTF - Posts: 69
Another update:

So, it is necessary to decode the messages correctly.

Here's a fully working sample Python client that connects to a localhost SC Relay Server to output whatever is being (and allowed to be) relayed.

You can use this code to try it out, it is mesmerizing to see all the console outputs. You don't need to install any extra dependencies since they are all included in standard Python installation.


import socket
import json
import threading
import time

def create_logon_request():
logon_request = {
"Type": 1,
"ProtocolVersion": 8,
"HeartbeatIntervalInSeconds": 10,
"Integer_1": 0x2,
}
return json.dumps(logon_request).encode('utf-8') + b'\x00'

def create_heartbeat():
heartbeat = {
"Type": 3,
}
return json.dumps(heartbeat).encode('utf-8') + b'\x00'

def send_heartbeat(sock):
while True:
try:
sock.send(create_heartbeat())
print("Heartbeat sent.")
except socket.error as e:
print("Socket error:", e)
break
time.sleep(10) # Send heartbeat every 10 seconds

def receive_response(sock):
response = b""
try:
while True:
part = sock.recv(1024)
if part:
response += part
# Check if there's more than one JSON message in the buffer
while b'\x00' in response:
pos = response.find(b'\x00') # Find the position of the null terminator
single_message = response[:pos] # Extract one JSON message
process_message(single_message.decode('utf-8')) # Decode and process this message
response = response[pos + 1:] # Remove the processed message from the buffer
else:
break
except socket.error as e:
print("Socket error:", e)

def process_message(message):
try:
msg_json = json.loads(message)
if msg_json['Type'] == 3:
print("Heartbeat received.")
elif msg_json['Type'] == 101:
print("Market data received:", msg_json)
elif msg_json['Type'] == 507:
symbol = json.dumps(msg_json)
print (symbol)

else:
print("Received message:", msg_json)
except json.JSONDecodeError as e:
print("Error decoding JSON:", e)
except KeyError:
print("Received unknown message type:", message)
except ValueError as e:
print("Value error:", e)

def main():
host = '127.0.0.1'
port = 11099
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
print("Connected to Sierra Chart Relay Server")

# Send logon request
s.send(create_logon_request())
print("Logon request sent.")

# Start the heartbeat thread
heartbeat_thread = threading.Thread(target=send_heartbeat, args=(s,))
heartbeat_thread.start()

# Handle responses
receive_response(s)

# Wait for the heartbeat thread to finish (if ever)
heartbeat_thread.join()

if __name__ == "__main__":
main()