jdf / Processing.py-Bugs

A home for all bugs and feature requests about Python Mode for the Processing Development Environment.
41 stars 8 forks source link

processing.net.* does not implement serverEvent #183

Closed anjchang closed 7 years ago

anjchang commented 7 years ago

I'm trying to rewrite the simple server code from Making Things Talk into pyprocessing. serverEvent doesn't seem to work, so I can't tell when clients have connected. Am I using serverEvent() wrong, or is this just not handled? Clients seem to just be able to connect just fine.

###a server that  listens for clients and prints what they say
###also sends the last client anything that's typed on the keyboard
###from Making things talk project 8

add_library( "net" )
port = 8080
myServer = None
clients = []

def setup():
    global myServer
    myServer = Server(this,port)
    println('Starting server on port 8080')

def draw():
    global myServer
    speakingClient = myServer.available()
    if (speakingClient != None):
        message = trim(speakingClient.readString())
        println(speakingClient.ip() + '\t' + message)

        if (message == 'exit'):
            clients.remove(speakingClient.ip())
            myServer.disconnect(speakingClient)

def serverEvent(myServer,thisClient):
    global clients
    println("we have a new client:" + thisClient.ip())
    clients.append(thisClient.ip())

def keyReleased():
    global myServer
    myServer.write(key)
anjchang commented 7 years ago

Nevermind, i just decided to avoid serverEvent and just use the client's messages to let me know if something was happening. It works for progressing with the rest of the making things talk tutorial.

""" a server that  listens for clents and prints what they say
also sends the last client anything that's typed on the keyboard
from Making things talk project 8
client ctrl+] mode character for streaming char
"""
add_library( "net" )
port = 8081
myServer = None
clients = []

def setup():
    global myServer
    myServer = Server(this,port)
    println('Starting server on port '+str(port))

def draw():
    global myServer
    speakingClient = myServer.available()
    if (speakingClient != None):
        message = trim(speakingClient.readString())
        if speakingClient.ip() not in clients:
            connectClient(speakingClient)
        println(speakingClient.ip() + '\t' + message)
        if (message == 'exit'):
            closeClient(speakingClient)

def closeClient(thisClient):
    global myServer,clients
    print thisClient.ip()+' disconnected'
    if thisClient.ip() in clients:
        clients.remove(thisClient.ip())
    myServer.disconnect(thisClient)

def connectClient(thisClient):
    println("we have a new client:" + str(thisClient.ip()))
    clients.append(thisClient.ip())

def keyReleased():
    global myServer
    myServer.write(key)