1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to use Sockets in pmmp

Discussion in 'Development' started by KSS-Dev, Jul 25, 2018.

  1. KSS-Dev

    KSS-Dev Spider

    Messages:
    11
    GitHub:
    kss-dev
    I have problems with sockets in PocketMine. I know what they are used for but actually I am still programming on my mobile since I get a new Computer. I don't know how to open and how do I implement it for example in PlayerChatEvent. If anyone have an example code for me that shoes me how I can broadcast messages through all my servers, it will be really helpful.

    Thanks :D
     
  2. tungstenvm

    tungstenvm Witch

    Messages:
    54
    did u get the answer
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    There are various client-server examples of this all over the internet. Here's one:
    P.S: You'll have to implement a security layer to avoid MITM attacks.
    Check out https://github.com/yesdog/Waterdog while you're at it.
     
    SleepSpace9 likes this.
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    http://xyproblem.info
    What are you really trying to do? I don't see a valid reason why you want to start a socket in each PlayerChatEvent call.
     
  5. tungstenvm

    tungstenvm Witch

    Messages:
    54
    i dont understand about socket much, idk the stream_socket_server() and other funcition on that code above are difference then socket_create(),etc or not. I want to send message from a server to another server with socket.The code below work but i have 2 problems
    1. "Accept client part "make server freeze,the sv work again if i use /irc in client server
    2. I was trying to find how to send message to all client in a socket
    3.Idk what will i change in $host and $port

    and i cant understand the code provided by muqsit. but the code below i can
    This is what i was trying:


    On server 1: which is like a host server
    • function onEnable(){
    • $host = "127.0.0.1";
    • $port = 25003;
    • // don't timeout!
    • set_time_limit(0);
    • // create socket
    • $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    • socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
    • // bind socket to port
    • $result = socket_bind($this->socket, $host, $port) or die("Could not bind to socket\n");
    • // start listening for connections
    • $result = socket_listen($this->socket, 3) or die("Could not set up socket listener\n");
    • var_dump($result);
    • }
    • ----------
    • this code is call from repeating task:
    • public function irc(){
    • // accept incoming connections
    • // spawn another socket to handle communication
    • var_dump($this->socket);
    • $spawn = socket_accept($this->socket) or die("Could not accept incoming connection\n");
    • // read client input
    • $input = socket_read($spawn, 1024) or die("Could not read input\n");
    • // clean up input string
    • $input = trim($input);
    • echo "Client Message : ".$input;
    • // reverse client input and send back
    • $output = strrev($input) . "\n";
    • socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
    • socket_close($this->socket);

    This is on sv 2,which is like a client sv :
    • onEnable(){
      $host = "127.0.0.1";
      $port = 25003;
      // create socket
      $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
      // connect to server
      $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
      }

    • onCommand(){
      $host = "127.0.0.1";
      $port = 25003;
      switch(strtolower($command->getName())){
      case "irc":
      $message = "xin chao";
      // create socket
      $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
      // connect to server
      $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");
      // send string to server
      socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
      // get server response
      $result = socket_read ($socket, 1024) or die("Could not read server response\n");
      echo "Reply From Server :".$result;
      }
      return true;
      }

      }
     
  6. KSS-Dev

    KSS-Dev Spider

    Messages:
    11
    GitHub:
    kss-dev
    Actually I was trying to code a system where I send a request to player xyz who is on another server under my network. Just like I would send the messages through proxy or something...
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.