-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: It would be nice if we can provide a service that supports [The WebSocket Protocol](https://datatracker.ietf.org/doc/html/rfc6455) Modifications: - Add `WebSocketService` and `WebSocketHandler` to implement the WebSocket service using back pressure. - Add `WebSocketFrame` that represents the web socket frames. - Add `RequestLogBuilder.responseCause(cause)` to set the cause while sending a normal response. - Forked `HttpServerCodec` from netty to support WebSocket upgrade. - Forked WebSocker encoder and decoder from Netty to use it without channels. Result: - You can now use `WebSocketService` to send WebSocket messages. ``` ServerBuilder sb = ... WebSocketHandler backpressureHandler = (ctx, messages) -> { WebSocketWriter webSocketWriter = WebSocket.streaming(); // Write frames using back pressure. return webSocketWriter; }; sb.service("/chat", WebSocketService.of(backpressureHandler)); ``` Todos: - Provide another abstract class for the `WebSocketHandler` so that a user can implement WebSocket easily without considering back pressure. (next PR) - Support WebSocketClient. (next PR) - Support extensions. (next PR)
- Loading branch information
Showing
76 changed files
with
5,432 additions
and
714 deletions.
There are no files selected for viewing
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
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
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
110 changes: 110 additions & 0 deletions
110
core/src/main/java/com/linecorp/armeria/common/websocket/ByteArrayWebSocketFrame.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,110 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common.websocket; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
import com.linecorp.armeria.common.annotation.Nullable; | ||
import com.linecorp.armeria.internal.common.ByteArrayBytes; | ||
|
||
class ByteArrayWebSocketFrame extends ByteArrayBytes implements WebSocketFrame { | ||
|
||
private static final byte[] EMPTY_BYTES = {}; | ||
|
||
static final WebSocketFrame EMPTY_PING = new ByteArrayWebSocketFrame(EMPTY_BYTES, WebSocketFrameType.PING); | ||
static final WebSocketFrame EMPTY_PONG = new ByteArrayWebSocketFrame(EMPTY_BYTES, WebSocketFrameType.PONG); | ||
|
||
private final WebSocketFrameType type; | ||
private final boolean finalFragment; | ||
|
||
@Nullable | ||
private String text; | ||
|
||
ByteArrayWebSocketFrame(byte[] array, WebSocketFrameType type) { | ||
this(array, type, true); | ||
} | ||
|
||
ByteArrayWebSocketFrame(byte[] array, WebSocketFrameType type, boolean finalFragment) { | ||
this(array, type, finalFragment, null); | ||
} | ||
|
||
ByteArrayWebSocketFrame(byte[] array, WebSocketFrameType type, | ||
boolean finalFragment, @Nullable String text) { | ||
super(array); | ||
this.type = type; | ||
this.finalFragment = finalFragment; | ||
this.text = text; | ||
} | ||
|
||
@Override | ||
public WebSocketFrameType type() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public boolean isFinalFragment() { | ||
return finalFragment; | ||
} | ||
|
||
@Override | ||
public String text() { | ||
if (text != null) { | ||
return text; | ||
} | ||
return text = toString(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return (super.hashCode() * 31 + type.hashCode()) * 31 + Boolean.hashCode(finalFragment); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof WebSocketFrame)) { | ||
return false; | ||
} | ||
|
||
final WebSocketFrame that = (WebSocketFrame) o; | ||
if (length() != that.length()) { | ||
return false; | ||
} | ||
|
||
return type == that.type() && | ||
finalFragment == that.isFinalFragment() && | ||
Arrays.equals(array(), that.array()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toString(super.toString()); | ||
} | ||
|
||
private String toString(String bytes) { | ||
return MoreObjects.toStringHelper(this).omitNullValues() | ||
.add("type", type) | ||
.add("finalFragment", finalFragment) | ||
.add("bytes", bytes) | ||
.toString(); | ||
} | ||
} |
Oops, something went wrong.