繁体   English   中英

Java和PHP套接字通信和AJAX

[英]Java and PHP socket communication and AJAX

我有一个Java应用程序正在检查文件系统中是否有更改-创建,删除和修改文件和文件夹。 它正在向PHP应用程序发送消息。 因此,这是Java应用程序正在运行的情况,我正在指定目录中创建一个新文件。 Java正在捕获该错误并将其显示在控制台上(用于测试目的),然后将其发送到PHP。 刷新时,我会看到消息,但是,如果不刷新页面并在指定目录中创建新文件,则Java既不会在控制台中也不显示消息,也不会将其发送给PHP。 因此,看来基本上在文件夹中的每个更改之后,我都必须刷新以捕获更改。 我想知道是否可以使用AJAX来避免每次更改后的刷新。 如果AJAX不是一个选项,或者它无法正常工作,还有另一种方法吗? 这是我的代码:

主要

package com.company;

import java.io.IOException;
import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

public class Main {

    public static final String DIRECTORY_TO_WATCH = "D:/Test";

    public static void main(String[] args) throws IOException, InterruptedException {
    // write your code here
        Path toWatch = Paths.get(DIRECTORY_TO_WATCH);
        CommunicationServer server = CommunicationServer.getInstance();
        if(toWatch == null) {
            throw new UnsupportedOperationException("Directory not found");
        }

        // make a new watch service that we can register interest in
        // directories and files with.
        WatchService myWatcher = toWatch.getFileSystem().newWatchService();
       // CommunicationServer server = new CommunicationServer();

        // start the file watcher thread below
        MyWatchQueueReader fileWatcher = new MyWatchQueueReader(myWatcher, server);
        Thread th = new Thread(fileWatcher, "FileWatcher");
        th.start();

        // register a file
        toWatch.register(myWatcher, ENTRY_CREATE, ENTRY_MODIFY);
        th.join();
    }
}

读者

package com.company;

import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

/**
 * Created by Ivan on 3/16/2016.
 */
public class MyWatchQueueReader implements Runnable {

    /** the watchService that is passed in from above */
    private WatchService myWatcher;
   private CommunicationServer server;
    public MyWatchQueueReader(WatchService myWatcher, CommunicationServer server) {
        this.myWatcher = myWatcher;
        this.server = server;
    }

    /**
     * In order to implement a file watcher, we loop forever
     * ensuring requesting to take the next item from the file
     * watchers queue.
     */
    @Override
    public void run() {
        try {
            // get the first event before looping
            WatchKey key = myWatcher.take();
            while(key != null) {
                // we have a polled event, now we traverse it and
                // receive all the states from it
                for (WatchEvent event : key.pollEvents()) {
                    System.out.printf("Received %s event for file: %s\n",
                            event.kind(), event.context() );
                    System.out.printf("Received\n");
                    String line = "Received " + event.kind() + " event for file: " + event.context() + "\n";
                    server.StartCommunicationg(line + "\n");
                    System.out.printf(" Not Received\n");
                }
                key.reset();
                key = myWatcher.take();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Stopping thread");
    }
}

服务器:

package com.company;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Ivan on 3/16/2016.
 */
public class CommunicationServer {
    private static int port = 20222;
    private static ServerSocket listenSock = null;
    private Socket sock = null;
    private static CommunicationServer instance = null;

    protected CommunicationServer() {
        System.out.println("Communication has started");
    }

    public static CommunicationServer getInstance() {
        if (instance == null) {
            instance = new CommunicationServer();
            try{
                listenSock = new ServerSocket(port);
            } catch (IOException ex){
                ex.printStackTrace();
            }
        }

        return instance;
    }

    public void StartCommunicationg(String message) {
        try {



            //while(true) {
                this.sock = listenSock.accept();

                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

                writer.write(message + "/n");
                writer.flush();
                writer.close();
                sock.close();
            //}
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }
}

的PHP

<?php

$PORT = 20222; //the port on which we are connecting to the "remote" machine
$HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)

$sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
        or die("error: could not create socket\n");

$succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
        or die("error: could not connect to host\n");

//$text = "Hello, Java!"; //the text we want to send to the server

//socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
        //or die("error: failed to write to socket\n");

$reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
        or die("error: failed to read from socket\n");

echo $reply;
?>

整个想法是我有一个文件夹,例如D:\\ Test。 不知道是否可以向用户显示其中是否有更改-仅使用PHP创建文件夹或文件。 因此,我决定使用Java查找文件系统中的更改。 发生更改时,用户应在浏览器中看到一条通知。如果PHP应用程序未运行,则Java应用程序应检查此文件夹中的更改,而当PHP应用程序再次运行时,它应收到所有文件夹中所做的更改。两个应用程序之间的通信存在很多问题,我想知道使用数据库是否会更好? 但是随之而来的问题是,PHP将如何知道数据库已更新...在这里变得非常困惑...

在这里的任何帮助将不胜感激。

问候,伊万

本质上,我不同意CommunicationServer Java类,最好使用Java Servlet,而不要使用套接字和PHP。

无论如何,如果要使用此策略,我不得不说,浏览器中加载的网页将不会自动刷新,并且需要按刷新键或添加AJAX代码,稍后可以加载。 例如,您可以使用这样的代码

setInterval(function(){
  // your code
}, 10000);

有关此范围内的更多信息,我强烈建议您查看此链接

试图了解您的问题,我认为您可能需要一个额外的过程。 AJAX可以提供帮助,但不能解决PHP需要持续旋转的问题。

您可以只使用AJAX来继续轮询PHP实例,但是我认为它将错过在轮询时间之间可能发生的任何文件更改事件,因为它们似乎没有被PHP会话捕获。

一种解决方案是WebSockets,它允许您根据事件将数据传递给用户,因此,您可以拥有一个Java应用程序的TCP套接字和一个最终用户的WebSocket,这将通过任何有关以下信息的发送文件发生更改时,但是支持有限,这可能很困难。

或者,您可以在后台运行的PHP服务器上有一个进程,将该信息提交到各种数据库中(由于它在同一服务器上运行,因此Java applet可以根据您的情况执行此操作),然后只需给条目加上时间戳,并让AJAX轮询PHP会话。 这样,无论轮询时间是什么,都不会丢失或丢失任何内容,并且转发的条目不再相关时,您可以删除它们。

无论哪种方式,在尝试将新信息通知客户端时,使用Web浏览器都很难解决。 如果您感到懒惰,则可以让PHP从不关闭会话,并继续将数据回显到浏览器。

暂无
暂无

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

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