Last active
August 1, 2023 00:36
-
-
Save catboy1357/bff4005d33eb564120ea16ef9eb69063 to your computer and use it in GitHub Desktop.
A code snippet for vrchat to send and receave at the same time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires the python-osc library. If you need to install the modual use: "pip install python-osc" | |
import threading | |
from pythonosc import osc_server, dispatcher, udp_client | |
# Makes a thread to run the server in the background. | |
def start_server(ip="127.0.0.1", port=9001): | |
print(f"Listening on {ip}:{port}") | |
server = osc_server.ThreadingOSCUDPServer((ip, port), dispatcher) | |
thread = threading.Thread(target=server.serve_forever) | |
thread.daemon = True | |
thread.start() | |
# Makes a thread for sending messages. | |
def send_message(address, value, ip="127.0.0.1", port=9000): | |
print(f"Send {address} : {value}") | |
client = udp_client.SimpleUDPClient(ip, port) | |
thread = threading.Thread(target=client.send_message, args=(address, value)) | |
thread.start() | |
# Call back function to handle incoming messages received by the server. | |
def receive_message(address, *args): | |
print(f"Get {address}:{args[0]}") | |
# Code here for receiving messages. | |
# The main body of the program. | |
def main(): | |
# Maps an address to the dispatcher and receives a callback function. | |
dispatcher.map("/avatar/change", receive_message) | |
start_server() | |
# Code here for the logic of the program. | |
# Example for sending a test message. | |
send_message("/test", 1) | |
# Sets up the dispatcher and starts the main function. | |
if __name__ == "__main__": | |
dispatcher = dispatcher.Dispatcher() | |
main() | |
# holds the program alive while the thread is running in the background. | |
input("Press Enter to quit... \n") | |
print("Quitting...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment