简体   繁体   English

无法与通过 sparkjava 创建的 websocket url 连接

[英]Not able to connect with websocket url created via sparkjava

I am trying to create a websocket using sparkjava framework.我正在尝试使用sparkjava框架创建 websocket。 Below is the code for create a websocket下面是创建 websocket 的代码

public final class MainWS {
static Map<Session, String> USER_SESSION_MAP = new ConcurrentHashMap<>();
static int nextUserNumber = 1;
public static void main(String[] args) {
    port(8090);
    webSocket("/echo", ChatWebSocketHandler.class);
    init();
}
public static void broadcastMessage(String sender, String message) {
    USER_SESSION_MAP.keySet().stream().filter(Session::isOpen).forEach(session -> {
        try {
            session.getRemote().sendString(String.valueOf(new JSONObject().put("userMessage", "message to pass")
                    .put("userlist", USER_SESSION_MAP.values())));
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

} }

Now the CharWebSocketHandler code is as below:现在 CharWebSocketHandler 代码如下:

    @WebSocket
public final class ChatWebSocketHandler {

    private String sender, msg;

    @OnWebSocketConnect
    private void onConnect(Session user) throws Exception {
        String username = "User" + MainWS.nextUserNumber++;
        MainWS.USER_SESSION_MAP.put(user, username);
        MainWS.broadcastMessage(sender = "Server", msg = (username + " joined the Main"));
    }

    @OnWebSocketClose
    private void onClose(Session user, int statusCode, String reason) {
        String username = MainWS.USER_SESSION_MAP.get(user);
        MainWS.USER_SESSION_MAP.remove(user);
        MainWS.broadcastMessage(sender = "Server", msg = (username + " left the Main"));
    }

    @OnWebSocketMessage
    private void onMessage(Session user, String message) {
        MainWS.broadcastMessage(sender = MainWS.USER_SESSION_MAP.get(user), msg = message);
    }
}

After running my MainWS program i am using rxjs for getting websocket connection via Angular运行我的 MainWS 程序后,我使用 rxjs 通过 Angular 获得 websocket 连接

The code is as below:代码如下:

export class WebsocketService {
  socket: WebSocketSubject<WSMessageService>
  constructor() {
    this.socket = new WebSocketSubject("ws://localhost:8090/echo");
    this.socket.subscribe(
      msg => {
        console.log(msg)
      },
      err => {
        console.log(err)
      },
      () => {
        console.log('complete')
      }
    );
  }
  public sendMessage(message: WSMessageService): void {
    this.socket.next(message)
  }
}

Now when i try to run my code i am getting error as below:现在,当我尝试运行我的代码时,我收到如下错误:

Firefox can't establish a connection to the server at ws://localhost:8090/echo error { target: WebSocket, isTrusted: true, srcElement: WebSocket, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, returnValue: true, defaultPrevented: false, composed: false, … } Firefox can't establish a connection to the server at ws://localhost:8090/echo error { target: WebSocket, isTrusted: true, srcElement: WebSocket, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, returnValue:真,defaultPrevented:假,组成:假,... }

Not at all sure where exactly i am doing wrong.完全不确定我到底在哪里做错了。 If anybody have any idea please let me know.如果有人有任何想法,请告诉我。

The issue is i made all private methods in ChatWebSocketHandler class.问题是我在 ChatWebSocketHandler class 中创建了所有私有方法。 After making all public method i am able to connect and get websocket object.完成所有公共方法后,我能够连接并获取 websocket object。

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

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