Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- try {
- $ws = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- if ($ws === false) {
- throw new Exception('socket_create() failed: '.socket_strerror(socket_last_error())."\n");
- }else{
- $host_ip = gethostbyname('0.0.0.0');
- socket_bind($ws, $host_ip, 2222);
- socket_listen($ws, 5);
- $connection = @socket_accept($ws);
- //read from websocket
- printf("< %s\n", socket_read($connection, 1000, PHP_NORMAL_READ));
- //send to client
- $data = "{var:server_value}\n";
- $byteOut = socket_write($connection, $data, strlen($data));
- if ($byteOut === false) {
- echo " Ошибка.";
- throw new Exception('socket_send() failed: '.socket_strerror(socket_last_error())."\n");
- } else {
- echo "отправлено " . $byteOut . " байт.\n";
- }
- }
- }catch (Exception $e) {
- echo "\nError: ".$e->getMessage();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement