@noita-ts/pollnet 0.3.8 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async.d.ts +17 -1
- package/dist/async.lua +85 -3
- package/dist/index.d.ts +9 -3
- package/package.json +1 -1
package/dist/async.d.ts
CHANGED
|
@@ -49,7 +49,6 @@ export declare class HttpResponse {
|
|
|
49
49
|
headers: string;
|
|
50
50
|
body: string;
|
|
51
51
|
constructor(status: number, headers: string, body: string);
|
|
52
|
-
/** @noSelf */
|
|
53
52
|
static read(socket: pollnet.Socket): HttpResponse;
|
|
54
53
|
/**
|
|
55
54
|
* Parse the raw header block into a map.
|
|
@@ -74,3 +73,20 @@ export declare class HttpClient {
|
|
|
74
73
|
*/
|
|
75
74
|
post(url: string, headers?: Record<string, string | string[]> | string, body?: string): Promise<HttpResponse>;
|
|
76
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Async WebSocket client built on top of an {@link AsyncReactor}.
|
|
78
|
+
* Messages are delivered via the `onmessage` callback, and errors via
|
|
79
|
+
* `onerror`.
|
|
80
|
+
* Call {@link send} to send a message, and {@link close} to close the
|
|
81
|
+
* connection and cleanup.
|
|
82
|
+
*/
|
|
83
|
+
export declare class WebSocket {
|
|
84
|
+
private socket;
|
|
85
|
+
timeout: number;
|
|
86
|
+
onmessage: ((msg: string) => void) | null;
|
|
87
|
+
onerror: ((msg: string) => void) | null;
|
|
88
|
+
constructor(reactor: AsyncReactor, url: string);
|
|
89
|
+
send(message: string): void;
|
|
90
|
+
send_binary(data: string): void;
|
|
91
|
+
close(): void;
|
|
92
|
+
}
|
package/dist/async.lua
CHANGED
|
@@ -111,7 +111,7 @@ function HttpResponse.prototype.____constructor(self, status, headers, body)
|
|
|
111
111
|
self.headers = headers
|
|
112
112
|
self.body = body
|
|
113
113
|
end
|
|
114
|
-
function HttpResponse.read(socket)
|
|
114
|
+
function HttpResponse.read(self, socket)
|
|
115
115
|
local msgs, ____error = socket:await_n(3)
|
|
116
116
|
if not msgs then
|
|
117
117
|
error(
|
|
@@ -159,9 +159,91 @@ function HttpClient.prototype.____constructor(self, reactor)
|
|
|
159
159
|
self.reactor = reactor
|
|
160
160
|
end
|
|
161
161
|
function HttpClient.prototype.get(self, url, headers)
|
|
162
|
-
return self.reactor:spawn(function() return ____exports.HttpResponse
|
|
162
|
+
return self.reactor:spawn(function() return ____exports.HttpResponse:read(pollnet.http_get(url, headers, false)) end)
|
|
163
163
|
end
|
|
164
164
|
function HttpClient.prototype.post(self, url, headers, body)
|
|
165
|
-
return self.reactor:spawn(function() return ____exports.HttpResponse
|
|
165
|
+
return self.reactor:spawn(function() return ____exports.HttpResponse:read(pollnet.http_post(url, headers, body, false)) end)
|
|
166
|
+
end
|
|
167
|
+
--- Async WebSocket client built on top of an {@link AsyncReactor}.
|
|
168
|
+
-- Messages are delivered via the `onmessage` callback, and errors via
|
|
169
|
+
-- `onerror`.
|
|
170
|
+
-- Call {@link send} to send a message, and {@link close} to close the
|
|
171
|
+
-- connection and cleanup.
|
|
172
|
+
____exports.WebSocket = __TS__Class()
|
|
173
|
+
local WebSocket = ____exports.WebSocket
|
|
174
|
+
WebSocket.name = "WebSocket"
|
|
175
|
+
function WebSocket.prototype.____constructor(self, reactor, url)
|
|
176
|
+
self.timeout = 3600
|
|
177
|
+
self.onmessage = nil
|
|
178
|
+
self.onerror = nil
|
|
179
|
+
self.socket = pollnet.open_ws(url)
|
|
180
|
+
reactor:spawn(function()
|
|
181
|
+
local timer = 0
|
|
182
|
+
while not not self.socket do
|
|
183
|
+
do
|
|
184
|
+
local ok = true
|
|
185
|
+
local msg
|
|
186
|
+
while true do
|
|
187
|
+
ok, msg = self.socket:poll()
|
|
188
|
+
if not ok or not msg then
|
|
189
|
+
break
|
|
190
|
+
end
|
|
191
|
+
timer = 0
|
|
192
|
+
local ____opt_0 = self.onmessage
|
|
193
|
+
if ____opt_0 ~= nil then
|
|
194
|
+
____opt_0(msg)
|
|
195
|
+
end
|
|
196
|
+
if not self.socket then
|
|
197
|
+
return
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
local ____ok_2 = ok
|
|
201
|
+
if ____ok_2 then
|
|
202
|
+
timer = timer + 1
|
|
203
|
+
____ok_2 = timer < self.timeout
|
|
204
|
+
end
|
|
205
|
+
if ____ok_2 then
|
|
206
|
+
coroutine.yield()
|
|
207
|
+
goto __continue39
|
|
208
|
+
end
|
|
209
|
+
timer = 0
|
|
210
|
+
self.socket:close()
|
|
211
|
+
local ____opt_3 = self.onerror
|
|
212
|
+
if ____opt_3 ~= nil then
|
|
213
|
+
____opt_3(msg or "timeout")
|
|
214
|
+
end
|
|
215
|
+
do
|
|
216
|
+
local i = 0
|
|
217
|
+
while i < 60 do
|
|
218
|
+
coroutine.yield()
|
|
219
|
+
i = i + 1
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
if not not self.socket then
|
|
223
|
+
self.socket = pollnet.open_ws(url)
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
::__continue39::
|
|
227
|
+
end
|
|
228
|
+
end)
|
|
229
|
+
end
|
|
230
|
+
function WebSocket.prototype.send(self, message)
|
|
231
|
+
local ____opt_5 = self.socket
|
|
232
|
+
if ____opt_5 ~= nil then
|
|
233
|
+
____opt_5:send(message)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
function WebSocket.prototype.send_binary(self, data)
|
|
237
|
+
local ____opt_7 = self.socket
|
|
238
|
+
if ____opt_7 ~= nil then
|
|
239
|
+
____opt_7:send_binary(data)
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
function WebSocket.prototype.close(self)
|
|
243
|
+
local ____opt_9 = self.socket
|
|
244
|
+
if ____opt_9 ~= nil then
|
|
245
|
+
____opt_9:close()
|
|
246
|
+
end
|
|
247
|
+
self.socket = nil
|
|
166
248
|
end
|
|
167
249
|
return ____exports
|
package/dist/index.d.ts
CHANGED
|
@@ -146,11 +146,17 @@ declare namespace pollnet {
|
|
|
146
146
|
callback?: (sock: Socket, addr: string) => void,
|
|
147
147
|
): this;
|
|
148
148
|
|
|
149
|
-
/** Sets a connection handler */
|
|
149
|
+
/** Sets a connection handler for server sockets */
|
|
150
150
|
on_connection(callback: (sock: Socket, addr: string) => void): this;
|
|
151
151
|
|
|
152
|
-
/**
|
|
153
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Polls for socket events.
|
|
154
|
+
* Returns [ok, message], where on success the message may or may not be
|
|
155
|
+
* available (non-blocking), and on failure the message is an error string.
|
|
156
|
+
*/
|
|
157
|
+
poll():
|
|
158
|
+
| LuaMultiReturn<[true, string | undefined]>
|
|
159
|
+
| LuaMultiReturn<[false, string]>;
|
|
154
160
|
|
|
155
161
|
/** Awaits a message (coroutine.yield) */
|
|
156
162
|
await():
|