Skip to content
DrSparky-2007 edited this page Jan 21, 2021 · 1 revision

Server class

The server class inherits from BaseSocketConnector and uses connect, disconnect, and message callbacks.

General usage

Importing:

from pyNetSocket import Server

Creating 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()

Callbacks

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

Clone this wiki locally