Пример #1
0
class PiDroidServer(Server):
    '''
    '''

    def onStart(self, ip, port):
        self.noOfClients = 0
        self.clients = []
        self.messenger = MessageHandler(port)
        print "Server has started.."

    def onStop(self):
        print "Server is stopping.."

    def onConnect(self, socket):
        self.noOfClients += 1
        self.clients.append(socket)
        print "Client connected to server.."
        print self.noOfClients, "clients are connected"

        # if not socket.username == "guest" + str(self.noOfClients):
        socket.username = "******" + str(self.noOfClients)
        '''
        socket.msgtype = "/all"
        socket.param = ""
        socket.ignBroadcast = False
        socket.invisible = False
        socket.send("To change it, use command /user")
        socket.send("For more commands, type /help")
        socket.send("Your username is " + socket.username)
        '''

    def onMessage(self, socket, message):
        print "Client ", socket.username, " sent: ", message

        reply = self.messenger.dispatch(message)

        if reply != None:
            socket.send(reply)

        # Signify all is well
        return True

    def onDisconnect(self, socket):
        self.noOfClients -= 1
        for index in range(0, len(self.clients)):
            if self.clients[index] == socket:
                del self.clients[index]
                break
        print socket.username + " has disconnected from server..",
        print self.noOfClients, " clients are connected"