示例#1
0
 def __init__(self):
     _my_server = WebSocketServer(
         "127.0.0.1",
         8467,
         on_data_receive=self.on_data_receive,
         on_connection_open=self.on_connection_open,
         on_error=self.on_error,
         on_connection_close=self.on_connection_close,
         on_server_destruct=self.on_server_destruct)
     _my_server.serve_forever()
示例#2
0
    def run(self):
        streaming = True

        def on_data_receive(client, data):
            if data == "Manager:Disconnect":
                serverStreaming.close_client(client)
                cam.release()

            elif data == "Manager:cam/320":
                cam.set(3, 320)
                cam.set(2, 240)

            elif data == "Manager:cam/640":
                cam.set(3, 640)
                cam.set(2, 480)

        def on_connection_open(client):
            print("Connection open")
            time.sleep(2)
            serverStreaming.send(client, "Name:R2D2")

            print("Starting streaming video")

            while streaming:
                ret, img = cam.read()
                ret, buffer = cv2.imencode('.jpg', img)
                my_image = base64.b64encode(buffer)
                serverStreaming.send(client, "Img:" + str(my_image))

        def on_error(exception):
            print("Errore: " + exception)

        def on_connection_close(client):
            print("Connessione chiusa")

        def on_server_destruct():
            print("Chiusura del server")
            pass

        print("Creating streaming server")
        serverStreaming = WebSocketServer(
            self.address,
            self.port,
            on_data_receive=on_data_receive,
            on_connection_open=on_connection_open,
            on_error=on_error,
            on_connection_close=on_connection_close)

        cam = cv2.VideoCapture(0)

        serverStreaming.serve_forever()
示例#3
0
    def test_echo_server_single_client(self):
        def on_data_receive(client, data):
            """Called by the WebSocketServer when data is received."""
            data += '!'
            server.send(client, data)

        def on_error(exception):
            """Called when the server returns an error
            """
            raise exception

        server = WebSocketServer("127.0.0.1",
                                 8467,
                                 on_data_receive=on_data_receive,
                                 on_error=on_error)
        server_thread = threading.Thread(target=server.serve_once,
                                         args=(),
                                         daemon=True)
        server_thread.start()

        print('Connected')
        ws = create_connection("ws://localhost:8467")
        ws.send("Hello, World")
        result = ws.recv()
        ws.close()

        self.assertEqual(result, "Hello, World!")
示例#4
0
def ws_factory():
    server = WebSocketServer(
        "localhost",
        4502,
    )

    return server
示例#5
0
def Start(ip, port):
    global Server
    Server = WebSocketServer(ip,
                             port,
                             on_connection_open=NewConnection,
                             on_connection_close=RemoveConnection,
                             on_data_receive=HandleData)
    Thread(target=Server.serve_forever).start()
示例#6
0
def on_connection_open(client):
    """Called by the WebSocketServer when a new connection is opened.
    """
    ws.send(client, "Welcome to the echo server!")


def on_error(exception):
    """Called when the server returns an error
    """
    raise exception


def on_connection_close(client):
    """Called by the WebSocketServer when a connection is closed."""
    ws.send(client, "Closing socket")


def on_server_destruct():
    """Called immediately prior to the WebSocketServer shutting down."""
    pass


ws = WebSocketServer("127.0.0.1",
                     8467,
                     on_data_receive=on_data_receive,
                     on_connection_open=on_connection_open,
                     on_error=on_error,
                     on_connection_close=on_connection_close)
ws.serve_forever()
示例#7
0
def getBotResp(msg):
    return BOT_PREFIX + str(chatbot.get_response(msg))


# Dictionary of users
# Key:(port, address), Value: User class instance
users = {}


class User():

    def __init__(self, ip, client_socket):
        self.ip = ip
        self.port = ip[0]
        self.address = ip[1]
        self.client_socket = client_socket


if __name__ == "__main__":

    # Train based on the english corpus
    chatbot.train("chatterbot.corpus.english")

    server = WebSocketServer(URL, PORT,
                            on_data_receive=on_data_receive,
                            on_connection_open=on_connection_open,
                            on_error=on_error,
                            on_connection_close=on_connection_close,
                            on_server_destruct=on_server_destruct)
    server.serve_forever()
示例#8
0
    def run(self):
        def on_data_receive(client, data):
            msg = str(data)
            msgType = msg.split(":")[0]
            if (msgType == "Macro"):
                msgSplit = msg.split(":")[1]
                if (msgSplit.split("/")[0] == "Stop"):
                    print("Stop")
                else:
                    count = int(msgSplit.split("/")[0])
                    for n in range(1, count + 1):
                        msgCommand = msgSplit.split("/")[n]
                        if (msgCommand == "Forward"):
                            print(msgCommand)
                            #self.motor.forward(35, 1)
                        elif (msgCommand == "Back"):
                            print(msgCommand)
                            # self.motor.back(35, 1)
                        elif (msgCommand == "Left"):
                            print(msgCommand)
                            # self.motor.left(45, 1)
                        elif (msgCommand == "Right"):
                            print(msgCommand)
                            # self.motor.right(45, 1)
            elif (msgType == "Controler"):
                msgSplit = msg.split(":")[1]
                if (msgSplit == "Forward"):
                    print(msgSplit)
                    #self.motor.forward(50, 1)
                elif (msgSplit == "Back"):
                    print(msgSplit)
                    #self.motor.back(50, 1)
                elif (msgSplit == "Right"):
                    print(msgSplit)
                    #self.motor.right(50, 1)
                elif (msgSplit == "Left"):
                    print(msgSplit)
                    #self.motor.left(50, 1)
            elif (msgType == "Manager"):
                msgSplit = msg.split(":")[1]
                self.conn.close()

        def on_connection_open(client):
            print("Connessione avviata")

        def on_error(exception):
            print("Errore: " + str(exception))

        def on_connection_close(client):
            print("Connessione chiusa")

        def on_server_destruct():
            print("Chiusura del server")
            pass

        print("Creating reciver server")
        serverReciver = WebSocketServer(
            self.address,
            self.port,
            on_data_receive=on_data_receive,
            on_connection_open=on_connection_open,
            on_error=on_error,
            on_connection_close=on_connection_close)

        serverReciver.serve_forever()