|
| 1 | +package com.annimon.ownlang.lib.modules; |
| 2 | + |
| 3 | +import com.annimon.ownlang.annotations.ConstantInitializer; |
| 4 | +import com.annimon.ownlang.exceptions.TypeException; |
| 5 | +import com.annimon.ownlang.lib.*; |
| 6 | +import io.socket.client.IO; |
| 7 | +import io.socket.client.Socket; |
| 8 | +import java.net.URISyntaxException; |
| 9 | + |
| 10 | +/** |
| 11 | + * socket.io module. |
| 12 | + * |
| 13 | + * @author aNNiMON |
| 14 | + */ |
| 15 | +@ConstantInitializer |
| 16 | +public final class socket implements Module { |
| 17 | + |
| 18 | + public static void initConstants() { |
| 19 | + Variables.define("EVENT_CONNECT", new StringValue(Socket.EVENT_CONNECT)); |
| 20 | + Variables.define("EVENT_CONNECTING", new StringValue(Socket.EVENT_CONNECTING)); |
| 21 | + Variables.define("EVENT_CONNECT_ERROR", new StringValue(Socket.EVENT_CONNECT_ERROR)); |
| 22 | + Variables.define("EVENT_CONNECT_TIMEOUT", new StringValue(Socket.EVENT_CONNECT_TIMEOUT)); |
| 23 | + Variables.define("EVENT_DISCONNECT", new StringValue(Socket.EVENT_DISCONNECT)); |
| 24 | + Variables.define("EVENT_ERROR", new StringValue(Socket.EVENT_ERROR)); |
| 25 | + Variables.define("EVENT_MESSAGE", new StringValue(Socket.EVENT_MESSAGE)); |
| 26 | + Variables.define("EVENT_PING", new StringValue(Socket.EVENT_PING)); |
| 27 | + Variables.define("EVENT_PONG", new StringValue(Socket.EVENT_PONG)); |
| 28 | + Variables.define("EVENT_RECONNECT", new StringValue(Socket.EVENT_RECONNECT)); |
| 29 | + Variables.define("EVENT_RECONNECTING", new StringValue(Socket.EVENT_RECONNECTING)); |
| 30 | + Variables.define("EVENT_RECONNECT_ATTEMPT", new StringValue(Socket.EVENT_RECONNECT_ATTEMPT)); |
| 31 | + Variables.define("EVENT_RECONNECT_ERROR", new StringValue(Socket.EVENT_RECONNECT_ERROR)); |
| 32 | + Variables.define("EVENT_RECONNECT_FAILED", new StringValue(Socket.EVENT_RECONNECT_FAILED)); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void init() { |
| 37 | + initConstants(); |
| 38 | + Functions.set("newSocket", socket::newSocket); |
| 39 | + } |
| 40 | + |
| 41 | + private static Value newSocket(Value... args) { |
| 42 | + Arguments.checkOrOr(1, 2, args.length); |
| 43 | + |
| 44 | + try { |
| 45 | + final String url = args[0].asString(); |
| 46 | + if (args.length == 1) { |
| 47 | + return new SocketValue(IO.socket(url)); |
| 48 | + } |
| 49 | + |
| 50 | + if (args[1].type() != Types.MAP) { |
| 51 | + throw new TypeException("Map expected in second argument"); |
| 52 | + } |
| 53 | + IO.Options options = parseOptions((MapValue) args[1]); |
| 54 | + return new SocketValue(IO.socket(url, options)); |
| 55 | + } catch (URISyntaxException ue) { |
| 56 | + return NumberValue.MINUS_ONE; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static class SocketValue extends MapValue { |
| 61 | + |
| 62 | + private final Socket socket; |
| 63 | + |
| 64 | + public SocketValue(Socket socket) { |
| 65 | + super(11); |
| 66 | + this.socket = socket; |
| 67 | + init(); |
| 68 | + } |
| 69 | + |
| 70 | + private void init() { |
| 71 | + set("close", this::close); |
| 72 | + set("connect", this::connect); |
| 73 | + set("connected", this::connected); |
| 74 | + set("disconnect", this::disconnect); |
| 75 | + set("emit", this::emit); |
| 76 | + set("hasListeners", this::hasListeners); |
| 77 | + set("id", this::id); |
| 78 | + set("off", this::off); |
| 79 | + set("on", this::on); |
| 80 | + set("once", this::once); |
| 81 | + set("open", this::open); |
| 82 | + set("send", this::send); |
| 83 | + } |
| 84 | + |
| 85 | + private Value close(Value... args) { |
| 86 | + socket.close(); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + private Value connect(Value... args) { |
| 91 | + socket.connect(); |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + private Value connected(Value... args) { |
| 96 | + return NumberValue.fromBoolean(socket.connected()); |
| 97 | + } |
| 98 | + |
| 99 | + private Value disconnect(Value... args) { |
| 100 | + socket.disconnect(); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + private Value hasListeners(Value... args) { |
| 105 | + Arguments.check(1, args.length); |
| 106 | + return NumberValue.fromBoolean( |
| 107 | + socket.hasListeners(args[0].asString()) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + private Value emit(Value... args) { |
| 112 | + Arguments.checkOrOr(2, 3, args.length); |
| 113 | + final String event = args[0].asString(); |
| 114 | + final Value value = args[1]; |
| 115 | + if (args.length == 3) { |
| 116 | + // TODO ack |
| 117 | + } |
| 118 | + socket.emit(event, ValueUtils.toObject(value)); |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + private Value id(Value... args) { |
| 123 | + return new StringValue(socket.id()); |
| 124 | + } |
| 125 | + |
| 126 | + private Value off(Value... args) { |
| 127 | + Arguments.checkOrOr(0, 1, args.length); |
| 128 | + if (args.length == 1) { |
| 129 | + socket.off(args[0].asString()); |
| 130 | + } else { |
| 131 | + socket.off(); |
| 132 | + } |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + private Value on(Value... args) { |
| 137 | + Arguments.check(2, args.length); |
| 138 | + final String event = args[0].asString(); |
| 139 | + final Function listener = ((FunctionValue) args[1]).getValue(); |
| 140 | + socket.on(event, sArgs -> { |
| 141 | + executeSocketListener(listener, sArgs); |
| 142 | + }); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + private Value once(Value... args) { |
| 147 | + Arguments.check(2, args.length); |
| 148 | + final String event = args[0].asString(); |
| 149 | + final Function listener = ((FunctionValue) args[1]).getValue(); |
| 150 | + socket.once(event, sArgs -> { |
| 151 | + executeSocketListener(listener, sArgs); |
| 152 | + }); |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + private Value open(Value... args) { |
| 157 | + socket.open(); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + private Value send(Value... args) { |
| 162 | + Arguments.check(1, args.length); |
| 163 | + socket.send(ValueUtils.toObject(args[0])); |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + private void executeSocketListener(Function listener, Object[] sArgs) { |
| 168 | + if (sArgs == null) { |
| 169 | + listener.execute(new ArrayValue(0)); |
| 170 | + } else { |
| 171 | + int size = sArgs.length; |
| 172 | + final Value[] fArgs = new Value[size]; |
| 173 | + for (int i = 0; i < size; i++) { |
| 174 | + fArgs[i] = ValueUtils.toValue(sArgs[i]); |
| 175 | + } |
| 176 | + listener.execute(new ArrayValue(fArgs)); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static IO.Options parseOptions(MapValue map) { |
| 182 | + final IO.Options result = new IO.Options(); |
| 183 | + map.ifPresent("forceNew", v -> result.forceNew = v.asInt() != 0); |
| 184 | + map.ifPresent("multiplex", v -> result.multiplex = v.asInt() != 0); |
| 185 | + map.ifPresent("reconnection", v -> result.reconnection = v.asInt() != 0); |
| 186 | + map.ifPresent("rememberUpgrade", v -> result.rememberUpgrade = v.asInt() != 0); |
| 187 | + map.ifPresent("secure", v -> result.secure = v.asInt() != 0); |
| 188 | + map.ifPresent("timestampRequests", v -> result.timestampRequests = v.asInt() != 0); |
| 189 | + map.ifPresent("upgrade", v -> result.upgrade = v.asInt() != 0); |
| 190 | + |
| 191 | + map.ifPresent("policyPort", v -> result.policyPort = v.asInt()); |
| 192 | + map.ifPresent("port", v -> result.port = v.asInt()); |
| 193 | + map.ifPresent("reconnectionAttempts", v -> result.reconnectionAttempts = v.asInt()); |
| 194 | + |
| 195 | + map.ifPresent("reconnectionDelay", v -> result.reconnectionDelay = getNumber(v).longValue()); |
| 196 | + map.ifPresent("reconnectionDelayMax", v -> result.reconnectionDelayMax = getNumber(v).longValue()); |
| 197 | + map.ifPresent("timeout", v -> result.timeout = getNumber(v).longValue()); |
| 198 | + |
| 199 | + map.ifPresent("randomizationFactor", v -> result.randomizationFactor = v.asNumber()); |
| 200 | + |
| 201 | + map.ifPresent("host", v -> result.host = v.asString()); |
| 202 | + map.ifPresent("hostname", v -> result.hostname = v.asString()); |
| 203 | + map.ifPresent("path", v -> result.path = v.asString()); |
| 204 | + map.ifPresent("query", v -> result.query = v.asString()); |
| 205 | + map.ifPresent("timestampParam", v -> result.timestampParam = v.asString()); |
| 206 | + |
| 207 | + map.ifPresent("transports", v -> { |
| 208 | + if (v.type() != Types.ARRAY) return; |
| 209 | + |
| 210 | + final ArrayValue arr = (ArrayValue) v; |
| 211 | + final String[] values = new String[arr.size()]; |
| 212 | + int index = 0; |
| 213 | + for (Value value : arr) { |
| 214 | + values[index++] = value.asString(); |
| 215 | + } |
| 216 | + result.transports = values; |
| 217 | + }); |
| 218 | + return result; |
| 219 | + } |
| 220 | + |
| 221 | + private static Number getNumber(Value value) { |
| 222 | + if (value.type() != Types.NUMBER) return value.asInt(); |
| 223 | + return ((NumberValue) value).raw(); |
| 224 | + } |
| 225 | +} |
0 commit comments