public class Server extends java.lang.Thread implements Deliverer
A callback is done every time a new connection is established, a connection is torn down or data is received if the PApplet invoking this server has the correct methods defined.
The appropriate methods in processing are:
void serverConnectEvent(Server serverInstance, RemoteAddress clientAddress);
void serverDisconnectEvent(Server serverInstance, RemoteAddress clientAddress);
void serverReceiveEvent(Server serverInstance, RemoteAddress clientAddress, String data);
void serverReceiveEvent(Server serverInstance, RemoteAddress clientAddress, byte[] data);
The serverReceiveEvent(...)
method with the String
argument will receive the data as an UTF-8 string. The version with a
byte[]
will receive the data without any conversions applied to
it.
The server itself runs in a separate thread.
RemoteAddress
,
Client
Modifier and Type | Field and Description |
---|---|
static int |
bufferSize |
Constructor and Description |
---|
Server(processing.core.PApplet parent,
int port)
This constructs a new Server.
|
Modifier and Type | Method and Description |
---|---|
void |
disconnectClient(RemoteAddress clientAddress)
This method is used to disconnect a specific client.
|
void |
dispose()
This method is called by the processing applet that contains this server
instance.
|
int |
getNumberOfConnectedClients()
This method returns the number of currently connected clients to the
server.
|
int |
getPort()
Returns the port that this server instance is listening for new
connections on
|
void |
run()
This method is doing the actual work of the server.
|
void |
sendTo(RemoteAddress address,
byte data)
This method sends data to a specified client identified by its
RemoteAddress.
|
void |
sendTo(RemoteAddress address,
byte[] data)
This method sends data to a specified client identified by its
RemoteAddress.
|
void |
sendTo(RemoteAddress address,
double data)
This method sends data to a specified client identified by its
RemoteAddress.
|
void |
sendTo(RemoteAddress address,
int data)
This method sends data to a specified client identified by its
RemoteAddress.
|
void |
sendTo(RemoteAddress address,
java.lang.String data)
This method sends data to a specified client identified by its
RemoteAddress.
|
void |
sendToAll(byte data)
This method sends data to all connected clients.
|
void |
sendToAll(byte[] data)
This method sends data to all connected clients.
|
void |
sendToAll(double data)
This method sends data to all connected clients.
|
void |
sendToAll(int data)
This method sends data to all connected clients.
|
void |
sendToAll(java.lang.String data)
This method sends data to all connected clients.
|
void |
throwReceiveEventByteArray(java.nio.ByteBuffer bf)
This is a helper method used to notify the encapsulating processing
applet that we have received data.
|
void |
throwReceiveEventString(java.nio.ByteBuffer bf)
This is a helper method used to notify the encapsulating processing
applet that we have received data.
|
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
public Server(processing.core.PApplet parent, int port)
parent
- the processing applet that contains the server instance.port
- the port that the server will listen for new connections onpublic int getPort()
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public void throwReceiveEventByteArray(java.nio.ByteBuffer bf)
throwReceiveEventByteArray
in interface Deliverer
bf
- the ByteBuffer containing the dataclientChannel
- the SocketChannel the data came frompublic void throwReceiveEventString(java.nio.ByteBuffer bf) throws java.nio.charset.CharacterCodingException
throwReceiveEventString
in interface Deliverer
bf
- the ByteBuffer containing the dataclientChannel
- the SocketChannel the data came fromjava.nio.charset.CharacterCodingException
public void sendToAll(java.lang.String data)
data
- the data to be sentpublic void sendToAll(int data)
data
- the data to be sentpublic void sendToAll(double data)
data
- the data to be sentpublic void sendToAll(byte data)
data
- the data to be sentpublic void sendToAll(byte[] data)
data
- the data to be sentpublic void sendTo(RemoteAddress address, java.lang.String data)
address
- the connected client's remote addressdata
- the data to be sentRemoteAddress
public void sendTo(RemoteAddress address, int data)
address
- the connected client's remote addressdata
- the data to be sentRemoteAddress
public void sendTo(RemoteAddress address, double data)
address
- the connected client's remote addressdata
- the data to be sentRemoteAddress
public void sendTo(RemoteAddress address, byte data)
address
- the connected client's remote addressdata
- the data to be sentRemoteAddress
public void sendTo(RemoteAddress address, byte[] data)
address
- the connected client's remote addressdata
- the data to be sentRemoteAddress
public void dispose()
public void disconnectClient(RemoteAddress clientAddress)
clientAddress
- the address of the client to disconnect.public int getNumberOfConnectedClients()