-
Notifications
You must be signed in to change notification settings - Fork 0
Server
DrSparky-2007 edited this page Jan 21, 2021
·
1 revision
The server class inherits from BaseSocketConnector and uses connect, disconnect, and message callbacks.
Importing:
from pyNetSocket import ServerCreating and running a server:
# Create a server object
my_server = Server(SERVER_IP, PORT)
# Start the server
my_server.start()When making a server, you can pass additional parameters too.
HEADER: Each message sent/received by the server can be up to 10^HEADER bytes long. It is 16 by default.
FORMAT: How to encode a message. It is "utf-8" by default.
DISCONNECT is a string. When a client sends this message, the client will be disconnected. It is "!disconnect"
Stopping a server:
my_server.stop()NOTE: The callbacks are not run on a thread, so things like infinite loops may freeze the rest of the functionality of the server. I'm working on fixing this
You can register a callback to run on an event by typing onEVENT (function, arguments, keyword_arguments). Replace EVENT with one of these events:
-
Connect: Run a function when a client connects -
Disconnect: Run a function when a client disconnects -
Message: Run a function when a client sends a message