【问题标题】:Deploy simple server code to Heroku将简单的服务器代码部署到 Heroku
【发布时间】:2013-05-10 13:24:01
【问题描述】:

我最近访问了 heroku.com 网站并尝试在那里部署我的第一个 java 程序,实际上我使用他们的 java 部署教程有一个良好的开端,并且运行正常。现在我有一个需要在那里部署的服务器代码,我尝试按照示例进行操作,但我有一些问题,例如,

1- 在这种情况下主机是什么,我已经尝试了应用程序链接,好像它是主机但它抛出错误,

这是我的示例服务器代码

public class DateServer {

    /** Runs the server. */
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(6780);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString());
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }
}

这是我的客户端代码

public class DateClient {

    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
    public static void main(String[] args) throws IOException {
        //I used my serverAddress is my external ip address 
        Socket s = new Socket(serverAddress, 6780);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
}

我在他们的网站上按照本教程https://devcenter.heroku.com/articles/java 上传了我的服务器代码,还有什么我需要做的吗?!

提前致谢

【问题讨论】:

    标签: java sockets heroku


    【解决方案1】:

    在 Heroku 上,您的应用程序必须 bind to the HTTP port provided in the $PORT environment variable。鉴于此,上述应用程序代码中的两个主要问题是 1) 您绑定到硬编码端口 (6780) 和 2) 您的应用程序使用 TCP 而不是 HTTP。如tutorial 所示,使用 Jetty 之类的东西来完成应用程序的 HTTP 等效,并使用 System.getenv("PORT") 绑定到正确的端口,如下所示:

    import java.util.Date;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.*;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.servlet.*;
    
    public class HelloWorld extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.getWriter().print(new Date().toString());
        }
    
        public static void main(String[] args) throws Exception{
            Server server = new Server(Integer.valueOf(System.getenv("PORT")));
            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/");
            server.setHandler(context);
            context.addServlet(new ServletHolder(new HelloWorld()),"/*");
            server.start();
            server.join();   
        }
    }
    

    【讨论】:

    • 好的,但是在这种情况下客户端代码会是一个正常的http连接吗?!这个实际上相当简单的例子,我真正在做的是构建多人游戏,客户端可以连接到服务器并玩所以我对你的服务器代码更改后的客户端代码感兴趣提前谢谢?请记住,我打算扩展代码以支持多人游戏,因此我们将不胜感激
    • 是的,您的客户端也需要使用 HTTP。 Heroku 应用程序可以建立出站 TCP 连接(例如到数据库),但所有入站连接都需要通过 HTTP。有关详细信息,请参阅info about Heroku HTTP routing
    猜你喜欢
    • 2022-10-26
    • 2013-09-07
    • 1970-01-01
    • 2014-11-10
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多