简体   繁体   English

套接字编程跨平台(PHP和Javascript)

[英]Socket Programming Cross Platform (PHP & Javascript)

I have created a socket server using PHP, the code is like this 我已经使用PHP创建了一个套接字服务器,代码是这样的

PHP 的PHP

function listen()
{
// set some variables
    $host = "127.0.0.1";
    $port = 25003;
// don't timeout!
    set_time_limit(0);
// create socket
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
    $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");

// accept incoming connections
// spawn another socket to handle communication
    $spawn = socket_accept($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."\n";
// reverse client input and send back
    $output = strrev($input) . "\n";
    socket_write($spawn, $output, strlen($output)) or die("Could not write output\n");
// close sockets
    socket_close($spawn);
    socket_close($socket);
    listen();
}

set_time_limit(0);
listen();

Taken from http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP 取自http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP

And I create the client socket using Javascript, like this 然后使用Javascript创建客户端套接字,如下所示

Javascript Java脚本

<!DOCTYPE HTML>
<html>
<head>
    <script type="text/javascript">
        function WebSocketTest()
        {
            if ("WebSocket" in window)
            {
                alert("WebSocket is supported by your Browser!");

                // Let us open a web socket
                var ws = new WebSocket("ws://localhost:25003");

                ws.onopen = function()
                {
                    // Web Socket is connected, send data using send()
                    ws.send("Message to send");
                    alert("Message is sent...");
                };

                ws.onmessage = function (evt)
                {
                    var received_msg = evt.data;
                    alert("Message is received...");
                };

                ws.onclose = function()
                {
                    // websocket is closed.
                    alert("Connection is closed...");
                };
            }

            else
            {
                // The browser doesn't support WebSocket
                alert("WebSocket NOT supported by your Browser!");
            }
        }
    </script>
</head>
<body>

<div id="sse">
    <a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>

</body>
</html>

Then I run the PHP server, it's work, it's listening. 然后,我运行PHP服务器,它正在工作,正在监听。 And the I run the Javascript client, it's work, the PHP Server is receiving request from Javascript. 然后,我运行Javascript客户端,它开始工作,PHP服务器正在接收来自Javascript的请求。

BUt the response from PHP is like this BUT来自PHP的响应是这样的

在此处输入图片说明

I don't understand how to change the client message, let's say I want to send message just like "Hello", 1. How can I call it from javascript so the PHP server is receive only "Hello" ? 我不了解如何更改客户端消息,比方说我想像“ Hello”一样发送消息。1.如何从javascript调用它,以便PHP服务器仅接收“ Hello”? 2. Anyone can explain to me what is the function of each url like this ? 2.任何人都可以向我解释这样的每个url的功能是什么?

ws://localhost:25003/daemon.php

what is the second parameter? 第二个参数是什么? is this connect to my php file? 这是连接到我的PHP文件? because I try to locate to my php it doesn't work. 因为我尝试定位到我的PHP,所以它不起作用。

PHP Socket server is OK, the problem is Javascript, it's sending a HTTP request and this send is a protocol for HTTP. PHP Socket服务器正常,问题是Javascript,它正在发送HTTP请求,而该发送是HTTP协议。

You can start the PHP Web Server on that port and answer that petisions whin Ajax queries. 您可以在该端口上启动PHP Web Server,并回答Ajax查询中的问题。

Command line to start php like a Web Server 像Web服务器一样启动php的命令行

php -S 127.0.0.1:25003

The document root for WebServer is the same for PHP WebServer的文档根目录与PHP相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM