简体   繁体   English

为什么我的websocket示例不起作用?

[英]Why my websocket example isn't working?

I have two files server.java and client.java. 我有两个文件server.java和client.java。 Basically there is no error or message like Client Connected, Connected to server, Message:..., etc. But if i change localhost to something else client.java does give an error but if it's correct no error or response. 基本上没有错误或消息,如“客户端已连接”,“已连接到服务器,消息:...”等。但是,如果我将localhost更改为其他内容,则client.java会给出错误,但如果正确,则没有错误或响应。

server.java has this: server.java具有:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.ArrayList;

    import javax.json.Json;
    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.ServerEndpoint;


    import org.glassfish.tyrus.server.*;

    @ServerEndpoint("/game")
    public class socketServer{

        public static void main(String[] args) {
            new socketServer();
        }

        public socketServer() {
            runServer();
        }

        public static void runServer() {
            Server s = new Server("localhost", 8025, "/websockets", null, socketServer.class);

            try {
                 s.start();
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
        }

        @OnMessage
        public String onMessage(String message, Session s) throws IOException {
            System.out.println("User input: " + message);
            s.getBasicRemote().sendText("Hello world Mr. " + message);
            return message;
        }

        @OnOpen
        public void onOpen(Session s) throws IOException {
            System.out.println("Client connected");
            s.getBasicRemote().sendText("wtf");
        }

        @OnClose
        public void onClose() {System.out.println("Connection closed");}

        @OnError
        public void handleError(Throwable t) {t.printStackTrace();}

    }

client.java has this: client.java具有:

import java.net.URI;
import java.net.URISyntaxException;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.DeploymentException;

import org.glassfish.tyrus.client.ClientManager;

@ClientEndpoint
public class clientServer {

    public static void main(String[] args) {
        new clientServer();
    }
    public clientServer() {
        ClientManager client = ClientManager.createClient();
        try {
            client.connectToServer(clientServer.class, new URI("ws://localhost:8025/websockets/game"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnOpen
    public void onOpen(Session session) {
           System.out.println("connected to server");
           try {
               session.getBasicRemote().sendText("start");
           } catch (IOException e) {
               throw new RuntimeException(e);
           }
    }

    @OnMessage
    public String onMessage(String message, Session session) {
        return message;
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        System.out.println("Client: closed");
    }


}

I guess server.java and client.java have to use the same host name. 我猜server.javaclient.java必须使用相同的主机名。 If you change the URI in client.java , for example, from "ws://localhost:8025/websockets/game" to "ws://myhost:8025/websockets/game" , try to change the first argument of the constructor of Server from "localhost" to "myhost" . 例如,如果您将client.java的URI从"ws://localhost:8025/websockets/game"更改为"ws://myhost:8025/websockets/game" ,请尝试更改Server"localhost""myhost"构造函数。

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

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