将一个简单的maven websocket tyrus项目转换为javac命令行

时间:2017-12-06 19:42:06

标签: websocket tyrus

出于好奇,我想在-classpath中使用javac和库构建一个简单的websockets tyrus服务器聊天项目。

是的,我知道这不是标准方式(mvn),但我将其作为概念证明。我设法建立项目,但它抛出NullPointerException

代码是:

ChatMain.java:

package chat;

import java.io.IOException; 
import java.util.Collections; 
import java.util.Map; 

import javax.websocket.DeploymentException; 

import org.glassfish.tyrus.server.Server; 

public class ChatMain { 

    public static void main(String[] args) throws DeploymentException, 
            IOException { 
        Map<String, Object> properties = Collections.emptyMap(); 
        Server server = new Server("localhost", 8080, "/ws", properties, 
                ChatEndPoint.class); 
        System.out.println(server);
        try { 
            server.start(); 
            System.in.read(); 
        } finally { 
            server.stop(); 
        } 
    } 
}

ChatEndPoint.java:

package chat;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import javax.websocket.OnClose; 
import javax.websocket.OnMessage; 
import javax.websocket.OnOpen; 
import javax.websocket.Session; 
import javax.websocket.server.ServerEndpoint; 
import org.json.*;

@ServerEndpoint("/chat") 
public class ChatEndPoint { 

    private static Map<String, Session> peers = Collections.synchronizedMap(new HashMap<String, Session>());

    @OnOpen 
    public void onOpen(Session peer) { 
        System.out.println("onOpen");
    } 

    @OnMessage 
    public void onMessage(String message, Session peer) throws IOException { 
        System.out.println("onMessage");
        JSONObject json = new JSONObject(message);
        String nick = (String) json.get("nick");
        boolean newClient = json.has("action") && ((String) json.get("action")).equals("add");
        synchronized(peers) {
            // Iterate over the connected sessions
            // and broadcast the received message
            for (Map.Entry<String, Session> entry : peers.entrySet()) {
                if (newClient) {
                    JSONObject json1 = new JSONObject(json, new String[] { "action" }).put("nick", entry.getKey());
                    peer.getBasicRemote().sendText(json1.toString());
                }
                entry.getValue().getBasicRemote().sendText(message);
            }
        }
        if (newClient)
            peers.put(nick, peer);
    } 

    @OnClose 
    public void onClose(Session peer) { 
        System.out.println("onClose");
        peers.values().remove(peer);
    } 
}

javac line:

javac -cp chat/javax.websocket-api-1.1.jar:chat/tyrus-server-1.12.jar:chat/tyrus-spi-1.12.jar:chat/tyrus-core-1.12.jar:chat/grizzly-framework-2.3.22.jar:chat/grizzly-http-server-2.3.22.jar:chat/tyrus-container-grizzly-server-1.12.jar:chat/json.jar: chat/ChatMain.java

java line:

java -cp chat/javax.websocket-api-1.1.jar:chat/tyrus-server-1.12.jar:chat/tyrus-spi-1.12.jar:chat/tyrus-core-1.12.jar:chat/grizzly-framework-2.3.22.jar:chat/grizzly-http-server-2.3.22.jar:chat/tyrus-container-grizzly-server-1.12.jar:chat/json.jar: chat.ChatMain

runtime error:

Exception in thread "main" java.lang.NullPointerException at org.glassfish.tyrus.container.grizzly.server.GrizzlyServerContainer$1.stop(GrizzlyServerContainer.java:228) at org.glassfish.tyrus.server.Server.stop(Server.java:231) at chat.ChatMain.main(ChatMain.java:23)

我明白这可以做到

1 个答案:

答案 0 :(得分:0)

有趣的是,需要更多的库。如果我在websocket-ri-archive-1.12.zip包中包含所有库,则效果很好。当然,建筑师是最佳选择