Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSO Websocket implementation. #300

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/websocket/BinaryType.java
@@ -0,0 +1,23 @@
/*
* Copyright 2017 Adam Ryan.
*
* Licensed 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
*
* http://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 org.teavm.jso.websocket;

/** A string indicating the type of binary data being transmitted by the connection. This should be
* either "blob" if DOM Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used. */
public enum BinaryType {
BLOB,
ARRAY_BUFFER,
}
133 changes: 133 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/websocket/CloseEvent.java
@@ -0,0 +1,133 @@
/*
* Copyright 2017 Adam Ryan.
*
* Licensed 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
*
* http://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 org.teavm.jso.websocket;

import java.util.HashMap;
import java.util.Map;
import org.teavm.jso.JSBody;
import org.teavm.jso.dom.events.Event;

public abstract class CloseEvent implements Event {
public enum Code {
RESERVED_UNUSED(
null,
"Reserved and not used."
),
CLOSE_NORMAL(
(short) 1000,
"Normal closure; the connection successfully completed whatever purpose for which it was created."
),
CLOSE_GOING_AWAY(
(short) 1001,
"The endpoint is going away, either because of a server failure or because the browser is navigating away"
+ " from the page that opened the connection."
),
CLOSE_PROTOCOL_ERROR((short) 1002, "The endpoint is terminating the connection due to a protocol error,"),
CLOSE_UNSUPPORTED((short) 1003, "The connection is being terminated because the endpoint received data of a"
+ " type it cannot accept (for example, a text-only endpoint received binary data),"),
RESERVED_1004((short) 1004, "Reserved. A meaning might be defined in the future,"),
CLOSE_NO_STATUS((short) 1005, "Reserved. Indicates that no status code was provided even though one was"
+ " expected,"),
CLOSE_ABNORMAL((short) 1006, "Reserved. Used to indicate that a connection was closed abnormally (that is, with"
+ " no close frame being sent) when a status code is expected,"),
Unsupported_Data((short) 1007, "The endpoint is terminating the connection because a message was received that"
+ " contained inconsistent data (e.g., non-UTF-8 data within a text message),"),
Policy_Violation((short) 1008, "The endpoint is terminating the connection because it received a message that"
+ " violates its policy. This is a generic status code, used when codes 1003 and 1009 are not"
+ " suitable,"),
CLOSE_TOO_LARGE((short) 1009, "The endpoint is terminating the connection because a data frame was received"
+ " that is too large,"),
Missing_Extension((short) 1010, "The client is terminating the connection because it expected the server to"
+ " negotiate one or more extension, but the server didn't,"),
Internal_Error((short) 1011, "The server is terminating the connection because it encountered an unexpected"
+ " condition that prevented it from fulfilling the request,"),
Service_Restart((short) 1012, "The server is terminating the connection because it is restarting. [Ref]"),
Try_Again_Later((short) 1013, "The server is terminating the connection due to a temporary condition, e.g. it"
+ " is overloaded and is casting off some of its clients. [Ref]"),
RESERVED_1014((short) 1014, "Reserved for future use by the WebSocket standard,"),
TLS_Handshake((short) 1015, "Reserved. Indicates that the connection was closed due to a failure to perform a"
+ " TLS handshake (e.g., the server certificate can't be verified),"),
RESERVED_FUTURE_USE(null, "Reserved for future use by the WebSocket standard,"),
RESERVED_EXTENSIONS(null, "Reserved for use by WebSocket extensions,"),
LIBRARY_SPECIFIED(null, "Available for use by libraries and frameworks. May not be used by applications."
+ " Available for registration at the IANA via first-come, first-serve,"),
APPLICATION_SPECIFIED(null, "Available for use by applications,"),
;
public final Short value;
public final String description;

Code(Short value, String description) {
this.value = value;
this.description = description;
}
}

private static final Map<Short, Code> codeEnums = new HashMap<Short, Code>() {
{
put(Code.CLOSE_NORMAL);
put(Code.CLOSE_GOING_AWAY);
put(Code.CLOSE_PROTOCOL_ERROR);
put(Code.CLOSE_UNSUPPORTED);
put(Code.RESERVED_1004);
put(Code.CLOSE_NO_STATUS);
put(Code.CLOSE_ABNORMAL);
put(Code.Unsupported_Data);
put(Code.Policy_Violation);
put(Code.CLOSE_TOO_LARGE);
put(Code.Missing_Extension);
put(Code.Internal_Error);
put(Code.Service_Restart);
put(Code.Try_Again_Later);
put(Code.RESERVED_1014);
put(Code.TLS_Handshake);
}

void put(Code code) {
put(code.value, code);
}
};

public Code codeAsEnum() {
short codeValue = code();
Code code = codeEnums.get(codeValue);
if (code == null) {
if (-1 < codeValue && codeValue < 1000) {
return Code.RESERVED_UNUSED;
} else if (1016 < codeValue && codeValue < 2000) {
return Code.RESERVED_FUTURE_USE;
} else if (codeValue < 3000) {
return Code.RESERVED_EXTENSIONS;
} else if (codeValue < 4000) {
return Code.LIBRARY_SPECIFIED;
} else if (codeValue < 5000) {
return Code.APPLICATION_SPECIFIED;
}
}
return code;
}

@JSBody(script = "return this.code;")
public abstract /*unsigned*/short code();

/** @return a String indicating the reason the server closed the connection. This is specific to the
* particular server and sub-protocol. */
@JSBody(script = "return this.reason;")
public abstract /*DOM*/String reason();

/** @return a boolean that indicates whether or not the connection was cleanly closed. */
@JSBody(script = "return this.wasClean;")
public abstract boolean wasClean();
}
@@ -0,0 +1,24 @@
/*
* Copyright 2017 Adam Ryan.
*
* Licensed 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
*
* http://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 org.teavm.jso.websocket;

import java.util.function.Consumer;

public interface ConsumerCallback<T> extends Consumer<T> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's good to extend Consumer

default void exception(Exception e) {
e.printStackTrace();
}
}
46 changes: 46 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/websocket/MessageEvent.java
@@ -0,0 +1,46 @@
/*
* Copyright 2017 Adam Ryan.
*
* Licensed 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
*
* http://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 org.teavm.jso.websocket;

import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
import org.teavm.jso.core.JSString;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.typedarrays.ArrayBuffer;

public abstract class MessageEvent implements Event {
/** @return the data sent by the message emitter. */
@JSBody(script = "return this.data;")
public native JSObject data();
@JSBody(script = "return this.data;")
public native JSString dataAsString();
@JSBody(script = "return this.data;")
public native ArrayBuffer dataAsArray();
/** @return a String representing the origin of the message emitter. */
@JSBody(script = "return this.origin;")
public native JSString origin();
/** @return a String representing a unique ID for the event. */
@JSBody(script = "return this.lastEventId;")
public native JSString lastEventId();
/** @return a MessageEventSource (which can be a WindowProxy, MessagePort, or ServiceWorker object) representing the
* message emitter. */
@JSBody(script = "return this.source;")
public native JSObject source(); //TODO
/** @return an array of MessagePort objects representing the ports associated with the channel the message is being
* sent through (where appropriate, e.g. in channel messaging or when sending a message to a shared worker). */
@JSBody(script = "return this.ports;")
public native JSObject[] ports(); //TODO
}
28 changes: 28 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/websocket/ReadyState.java
@@ -0,0 +1,28 @@
/*
* Copyright 2017 Adam Ryan.
*
* Licensed 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
*
* http://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 org.teavm.jso.websocket;

/** The current state of the connection. See individual enum's for specific meanings. */
public enum ReadyState {
/** The connection is not yet open. */
CONNECTING,
/** The connection is open and ready to communicate. */
OPEN,
/** The connection is in the process of closing. */
CLOSING,
/** The connection is closed or couldn't be opened. */
CLOSED,
}