forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketMessage.java
More file actions
52 lines (47 loc) · 1.41 KB
/
WebSocketMessage.java
File metadata and controls
52 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import io.jooby.internal.WebSocketMessageImpl;
import javax.annotation.Nonnull;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
/**
* Websocket message generated from a {@link WebSocket.OnMessage} callback. Message is a subclass.
*
* @author edgar
* @since 2.2.0
*/
public interface WebSocketMessage extends Value {
/**
* Convert this value to the given type. Support values are single-value, array-value and
* object-value. Object-value can be converted to a JavaBean type.
*
* @param type Type to convert.
* @param <T> Element type.
* @return Instance of the type.
*/
@Nonnull <T> T to(@Nonnull Type type);
/**
* Creates a websocket message.
*
* @param ctx HTTP context.
* @param bytes Text message as byte array.
* @return A websocket message.
*/
static @Nonnull WebSocketMessage create(@Nonnull Context ctx, @Nonnull byte[] bytes) {
return new WebSocketMessageImpl(ctx, bytes);
}
/**
* Creates a websocket message.
*
* @param ctx HTTP context.
* @param message Text message.
* @return A websocket message.
*/
static @Nonnull WebSocketMessage create(@Nonnull Context ctx, @Nonnull String message) {
return new WebSocketMessageImpl(ctx, message.getBytes(StandardCharsets.UTF_8));
}
}