p2p_connector.wifi_direct_socket_impl
This module provides an example application to show how the abstract class WifiDirectSocket could be used to implement application which communicates with an android phone using a P2P connection.
1""" 2This module provides an example application to show how the abstract class WifiDirectSocket could be used to implement 3application which communicates with an android phone using a P2P connection. 4""" 5import click 6from click_shell import shell 7 8from p2p_connector.wifi_direct_socket import WifiDirectSocket 9 10 11class SimpleMessenger(WifiDirectSocket): 12 """ 13 This class inherits from the abstract parent class WifiDirectSocket. This is a simple example how the class 14 WifiDirectSocket can be used to establish a P2P connection. 15 """ 16 17 def on_receive_message(self, message: bytes): 18 print(f'received message: {message.decode()}') 19 20 def on_client_connected(self): 21 print('client connected') 22 23 def on_client_disconnected(self): 24 print('client has disconnected') 25 26 def send_text_message(self, message: str): 27 """ 28 Sends a message to the client. 29 @param message: message as str 30 """ 31 self.send_message_to_client(f'{message}\n'.encode()) 32 33 34messenger = SimpleMessenger('192.168.4.1', 4444) 35 36 37@shell(prompt='my-app > ', intro='Starting my app...') 38def main(): 39 messenger.start_server_socket() 40 41 42@main.command() 43@click.option("--message", prompt=" Enter the message", type=str) 44def send(message): 45 click.echo(f"sending message '{message}'") 46 messenger.send_text_message(message) 47 48 49@main.command() 50def stop(): 51 click.echo("exiting") 52 messenger.stop_receive_thread() 53 54 55if __name__ == "__main__": 56 main()
12class SimpleMessenger(WifiDirectSocket): 13 """ 14 This class inherits from the abstract parent class WifiDirectSocket. This is a simple example how the class 15 WifiDirectSocket can be used to establish a P2P connection. 16 """ 17 18 def on_receive_message(self, message: bytes): 19 print(f'received message: {message.decode()}') 20 21 def on_client_connected(self): 22 print('client connected') 23 24 def on_client_disconnected(self): 25 print('client has disconnected') 26 27 def send_text_message(self, message: str): 28 """ 29 Sends a message to the client. 30 @param message: message as str 31 """ 32 self.send_message_to_client(f'{message}\n'.encode())
This class inherits from the abstract parent class WifiDirectSocket. This is a simple example how the class WifiDirectSocket can be used to establish a P2P connection.
def
on_receive_message(self, message: bytes):
Is called when a new message was received. @param message: received message (bytes)
def
on_client_disconnected(self):
is called if the client has been disconnected from the server socket.