forked from deephaven/deephaven-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client and server should each support both ws mechanisms
- Loading branch information
Showing
6 changed files
with
233 additions
and
81 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...-servlet-websocket-jakarta/src/main/java/io/grpc/servlet/web/websocket/GrpcWebsocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.grpc.servlet.web.websocket; | ||
|
||
import jakarta.websocket.CloseReason; | ||
import jakarta.websocket.Endpoint; | ||
import jakarta.websocket.EndpointConfig; | ||
import jakarta.websocket.Session; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Supports both grpc-websockets and grpc-websockets-multiplex subprotocols, and delegates to the correct implementation | ||
* after protocol negotiation. | ||
*/ | ||
public class GrpcWebsocket extends Endpoint { | ||
private final Map<String, Supplier<Endpoint>> endpointFactories = new HashMap<>(); | ||
private Endpoint endpoint; | ||
|
||
public GrpcWebsocket(Map<String, Supplier<Endpoint>> endpoints) { | ||
endpointFactories.putAll(endpoints); | ||
} | ||
|
||
public void onOpen(Session session, EndpointConfig endpointConfig) { | ||
Supplier<Endpoint> supplier = endpointFactories.get(session.getNegotiatedSubprotocol()); | ||
if (supplier == null) { | ||
try { | ||
session.close(new CloseReason(CloseReason.CloseCodes.PROTOCOL_ERROR, "Unsupported subprotocol")); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
return; | ||
} | ||
|
||
endpoint = supplier.get(); | ||
endpoint.onOpen(session, endpointConfig); | ||
} | ||
|
||
@Override | ||
public void onClose(Session session, CloseReason closeReason) { | ||
endpoint.onClose(session, closeReason); | ||
} | ||
|
||
@Override | ||
public void onError(Session session, Throwable thr) { | ||
endpoint.onError(session, thr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.