The Callbacks: onWebSocketOpen/Close

This part will be a little shorter as we will just take a look at some of the other callbacks of the webServerDAT. onWebSocketOpen and onWebSockertClose will be called the moment we connect to our server, giving as a string representation of the client.
We already have a way to check if the connection is established in the browser. With this callbacks we can also do that from TouchDesigner. Lets even go one step further and create a simple representation of the current selected clients.

Lets create an empty tableDAT in the network. We then will add a row with the clients information on connect and remove it on disconnect using the appendRow(client) and deleteRow(client) .

def onWebSocketOpen(webServerDAT, client, uri):
	op('table1').appendRow(client)
	return

def onWebSocketClose(webServerDAT, client):
	op('table1').deleteRow(client)
	return
I even opened a second tab with the same address in the browser and we got two clients in our list.

The complete content of the webServerCallbacks now are:

def onHTTPRequest(webServerDAT, request, response):
	response['statusCode'] = 404 #Missing File
	response['statusReason'] = 'Operator does not exist'
	target_operator = op( request['uri'] )
	if target_operator is None: return response
	
	if target_operator.isDAT: response['data'] = target_operator.text
	if target_operator.isTOP: response['data'] = target_operator.saveByteArray('.png')
	if 'data' in response:
		response['statusCode'] = 200 # OK
		response['statusReason'] = 'OK'
	
	return response

def onWebSocketOpen(webServerDAT, client, uri):
	op('table1').appendRow(client)
	return

def onWebSocketClose(webServerDAT, client):
	op('table1').deleteRow(client)
	return

def onWebSocketReceiveText(webServerDAT, client, data):
	webServerDAT.webSocketSendText(client, data)
	return

def onWebSocketReceiveBinary(webServerDAT, client, data):
	webServerDAT.webSocketSendBinary(client, data)
	return

def onWebSocketReceivePing(webServerDAT, client, data):
	return

def onWebSocketReceivePong(webServerDAT, client, data):
	return

def onServerStart(webServerDAT):
	return

def onServerStop(webServerDAT):
	return