그놈의 안드로이드
Vert.x 웹서버 구성 본문
1. Vert.x Examples 주소
-> https://github.com/vert-x3/vertx-examples
2. Intllij로 open 한다.
3. chat 프로젝트
#server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | package io.vertx.example.web.chat; import io.vertx.core.AbstractVerticle; import io.vertx.core.eventbus.EventBus; import io.vertx.example.util.Runner; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.StaticHandler; import io.vertx.ext.web.handler.sockjs.BridgeOptions; import io.vertx.ext.web.handler.sockjs.PermittedOptions; import io.vertx.ext.web.handler.sockjs.SockJSHandler; import java.sql.*; import java.text.DateFormat; import java.time.Instant; import java.util.Date; /** * A {@link io.vertx.core.Verticle} which implements a simple, realtime, * multiuser chat. Anyone can connect to the chat application on port * 8000 and type messages. The messages will be rebroadcast to all * connected users via the @{link EventBus} Websocket bridge. * * @author <a href="https://github.com/InfoSec812">Deven Phillips</a> */ public class Server extends AbstractVerticle { // Convenience method so you can run it in your IDE public static void main(String[] args) { Runner.runExample(Server.class); } @Override public void start() throws Exception { Router router = Router.router(vertx); String aaa = getDB(); // Allow events for the designated addresses in/out of the event bus bridge BridgeOptions opts = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("chat.to.server")) .addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client")); // Create the event bus bridge and add it to the router. SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts); router.route("/eventbus/*").handler(ebHandler); // router.route("/").handler(routingContext -> { // routingContext.response().putHeader("content-type", "text/html").end(aaa); // }); // Create a router endpoint for the static contentd router.route().handler(StaticHandler.create()); // Start the web server and tell it to use the router to handle requests. vertx.createHttpServer().requestHandler(router).listen(8080); EventBus eb = vertx.eventBus(); // Register to listen for messages coming IN to the server eb.consumer("chat.to.server").handler(message -> { // Create a timestamp string String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now())); // Send the message back out to all clients with the timestamp prepended. eb.publish("chat.to.client", timestamp + ": " + message.body()); }); } public static String getDB() { // Connection conn = null; // Statement stmt = null; String dbdata = ""; // try { // Class.forName("com.mysql.jdbc.Driver").newInstance(); // } catch (ClassNotFoundException e) { // System.out.println("드라이버 연결 에러."); // } catch (Exception etc) { // System.out.println(etc.getMessage()); // } // try { // String url = "jdbc:mysql://localhost/nettydb"; // String userId = "goo"; // String userPass = "1234"; // // conn = DriverManager.getConnection(url, userId, userPass); // stmt = conn.createStatement(); // ResultSet rs = null; // rs = stmt.executeQuery("select * from test"); // if (stmt.execute("select * from test")) { // rs = stmt.getResultSet(); // } // // while (rs.next()) { // int num = rs.getInt("num"); // System.out.println(num + "//"); // dbdata = "num = "+num+" "; // } // stmt.close(); // conn.close(); // } catch (SQLException e) { // System.out.println("SQLException : " + e.getMessage()); // } Connection con = null; Statement stmt = null; String server = "localhost"; // MySQL 서버 주소 String database = "vertxdb"; // MySQL DATABASE 이름 String user_name = "goo"; // MySQL 서버 아이디 String password = "1234"; // MySQL 서버 비밀번호 // 1.드라이버 로딩 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.err.println(" !! <JDBC 오류> Driver load 오류: " + e.getMessage()); e.printStackTrace(); } // 2.연결 try { con = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database + "?useSSL=false", user_name, password); System.out.println("정상적으로 연결었습니다."); stmt = con.createStatement(); ResultSet rs = null; rs = stmt.executeQuery("select * from test"); if(stmt.execute("select * from test")){ rs = stmt.getResultSet(); } while (rs.next()){ int num = rs.getInt("num"); String name = rs.getString("name"); dbdata = dbdata+"num = "+num+", name = "+name+" "; } stmt.close(); con.close(); } catch(SQLException e) { System.err.println("con 오류:" + e.getMessage()); e.printStackTrace(); } // 3.해제 try { if(con != null) con.close(); } catch (SQLException e) {} return dbdata; } } | cs |
4. 해당 디렉토리 /webroot 에 이미지를 집어넣는다.
5. 프로젝트 Run 한번 때려준다.
6. helloworld 프로젝트
#Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | package io.vertx.example.web.helloworld; import io.vertx.core.AbstractVerticle; import io.vertx.example.util.Runner; import io.vertx.ext.web.Router; import java.sql.*; /* * @author <a href="http://tfox.org">Tim Fox</a> */ public class Server extends AbstractVerticle { // Convenience method so you can run it in your IDE public static void main(String[] args) { Runner.runExample(Server.class); } @Override public void start() throws Exception { Router router = Router.router(vertx); router.route("/big").handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end( "<html>" + "<body>" + "<img src='big.jpg' width=200>" + "</body>" + "</html>"); }); router.route("/small1").handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end( "<html>" + "<body>" + "<img src='small1.jpg' width=200>" + "</body>" + "</html>"); }); router.route("/small2").handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end( "<html>" + "<body>" + "<img src='small2.jpg' width=200>" + "</body>" + "</html>"); }); router.route("/text").handler(routingContext -> { String dbdata = getDB(); routingContext.response().putHeader("content-type", "text/html").end( "<html>" + "<body>" + dbdata + "</body>" + "</html>"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } public static String getDB() { String dbdata = ""; Connection con = null; Statement stmt = null; String server = "localhost"; // MySQL 서버 주소 String database = "tn"; // MySQL DATABASE 이름 String user_name = "root"; // MySQL 서버 아이디 String password = "1234"; // MySQL 서버 비밀번호 // 1.드라이버 로딩 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.err.println(" !! <JDBC 오류> Driver load 오류: " + e.getMessage()); e.printStackTrace(); } // 2.연결 try { con = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database + "?useSSL=false", user_name, password); System.out.println("정상적으로 연결었습니다."); stmt = con.createStatement(); ResultSet rs = null; //rs = stmt.executeQuery("select * from tn"); if(stmt.execute("select * from tn")){ rs = stmt.getResultSet(); } while (rs.next()){ String name = rs.getString("name"); dbdata = dbdata + name; } stmt.close(); con.close(); } catch(SQLException e) { System.err.println("con 오류:" + e.getMessage()); e.printStackTrace(); } // 3.해제 try { if(con != null) con.close(); } catch (SQLException e) {} return dbdata; } } | cs |
7. 그리고 Run 해주면 된다
필자는 4개의 파라미터로 구분해 놨다.
1. /big
2. /small1
3. /small2
4. /text
'리눅스 > 웹 서버' 카테고리의 다른 글
Lighttpd error code 500 (0) | 2018.12.31 |
---|---|
Tornado 웹서버 구성 (0) | 2018.12.30 |
Node.js 웹서버 구성 (0) | 2018.12.30 |
Netty 웹서버 구성 (0) | 2018.12.30 |
H2o 설치 (0) | 2018.12.30 |