代码之家  ›  专栏  ›  技术社区  ›  Program-Me-Rev

如何将数据从节点发送到另一个服务器

  •  0
  • Program-Me-Rev  · 技术社区  · 6 年前

    我在想办法 节点 ,然后节点服务器将通过注册的套接字向特定的应用程序发送一个JSON数据包。

    这在Node中是可能的吗?如果不是,我如何用Javascript实现它,或者最好的实现方法是什么?

    这是Android应用服务器

    public class AndroidAppLocalServer {
        Activity activity;
        ServerSocket serverSocket;
        String message = "";
        static final int socketServerPORT = 8080;
    
        public AndroidAppLocalServer(Activity activity) {
            this.activity = activity;
            Thread socketServerThread = new Thread(new SocketServerThread());
            socketServerThread.start();
        }
    
        public int getPort() {
            return socketServerPORT;
        }
    
        public void onDestroy() {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        private class SocketServerThread extends Thread {
    
            int count = 0;
    
            @Override
            public void run() {
                try {
                    // create ServerSocket using specified port
                    serverSocket = new ServerSocket(socketServerPORT);
    
                    while (true) {
                        // block the call until connection is created and return
                        // Socket object
                        Socket socket = serverSocket.accept();
                        count++;
                        message += "#" + count + " from "
                                + socket.getInetAddress() + ":"
                                + socket.getPort() + "\n";
    
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.v("MyApp", message);
                            }
                        });
    
                        SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(socket, count);
                        socketServerReplyThread.run();
    
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        private class SocketServerReplyThread extends Thread {
    
            private Socket hostThreadSocket;
            int cnt;
    
            SocketServerReplyThread(Socket socket, int c) {
                hostThreadSocket = socket;
                cnt = c;
            }
    
            @Override
            public void run() {
                OutputStream outputStream;
                String msgReply = "Hello from AndroidAppLocalServer, you are #" + cnt;
    
                try {
                    outputStream = hostThreadSocket.getOutputStream();
                    PrintStream printStream = new PrintStream(outputStream);
                    printStream.print(msgReply);
                    printStream.close();
    
                    message += "replayed: " + msgReply + "\n";
    
                    activity.runOnUiThread(new Runnable() {
    
                        @Override
                        public void run() {
                            Log.v("MyApp", message);
                        }
                    });
    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    message += "Something wrong! " + e.toString() + "\n";
                }
    
                activity.runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        Log.v("MyApp", message);
                    }
                });
            }
    
        }
    
        public String getIpAddress() {
            String ip = "";
            try {
                Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (enumNetworkInterfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
                    Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
                    while (enumInetAddress.hasMoreElements()) {
                        InetAddress inetAddress = enumInetAddress.nextElement();
    
                        if (inetAddress.isSiteLocalAddress()) {
                            ip += "AndroidAppLocalServer running at : " + inetAddress.getHostAddress();
                        }
                    }
                }
    
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ip += "Something Wrong! " + e.toString() + "\n";
            }
            return ip;
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   0x530302    6 年前

    是的,您可以在Node.js中这样做,假设应用程序运行在实际上可以公开访问的手机上。由于您在Android应用程序中使用的是普通的TCP套接字,因此可以首先通过使用诸如 netcat telnet netcat <PUBLIC-IP> 8080 ).

    如果可以,您可以在Node.js中使用 net.Socket

    const net = require('net');
    const client = new net.Socket();
    client.connect(8080, '<PUBLIC-IP>', () => {
        // callback, when connection successfull
        client.write('Data sent to the App');
    });
    client.on('data', (data) => {
        // callback, when app replies with data
    });
    client.on('close', (data) => {
        // callback, when socket is closed
    });