Skip to content

Commit

Permalink
Fixed #327 (hopefully), closed #337
Browse files Browse the repository at this point in the history
Also fixed keys repeating after holding Ctrl+R/S/T.

The WebSocket server API now uses a new function, `websocketServer`. This function
initiates a server on the specified port, and returns a handle for listening
and closing the server.

The server no longer automatically closes when clients disconnect - the server
must close itself through the handle.

Clients are detected through the websocket_server_connect event, which
the server handle's `listen` function wraps around. This event contains the port
of the server that was connected, and the handle for this connection. The handle
has the same methods as a normal handle, but also contains a `clientID` field
that uniquely identifies this connection/client.

When a server connection receives a message, it sends the `websocket_server_message`
event, which consists of the client ID, the message, and whether the message
is a binary message. Likewise, when the connection is closed, the
`websocket_server_closed` message is sent with the client ID, and optionally the
code the connection was closed with.
  • Loading branch information
MCJack123 committed Dec 30, 2023
1 parent 1fd9f1c commit 98e4ece
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 118 deletions.
2 changes: 1 addition & 1 deletion api/Computer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct Computer {
std::mutex openWebsocketsMutex;
std::vector<std::pair<lua_CFunction, void*>> startupCallbacks; // List of functions to call when starting up + a userdata to pass as the first argument

// The following fields are available in API version 10.9 and later.
// The following fields are available in API version 12.0 and later.
std::vector<std::filesystem::path> droppedFiles; // List of files that were dropped in the current drop set

private:
Expand Down
2 changes: 0 additions & 2 deletions api/peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class peripheral {
virtual int call(lua_State *L, const char * method)=0;
// This function is deprecated, and no longer works. In fact, it did not work
// in any version of the API. Just leave this as-is.
#if __cplusplus >= 201402L
[[deprecated]]
#endif
virtual void update() {}
// This function should return a library_t containing the names of all of the
// methods available to the peripheral. Only the keys, name, and size members
Expand Down
2 changes: 1 addition & 1 deletion craftos2-lua
20 changes: 14 additions & 6 deletions src/Computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ void runComputer(Computer * self, const path_t& bios_name, const std::string& bi
lua_setfield(L, -2, "addListener");
lua_pushnil(L);
lua_setfield(L, -2, "removeListener");
lua_pushnil(L);
lua_setfield(L, -2, "websocketServer");
lua_pop(L, 1);
}
lua_getglobal(L, "debug");
Expand All @@ -557,14 +559,20 @@ void runComputer(Computer * self, const path_t& bios_name, const std::string& bi
lua_pop(L, 1);
}
if (config.serverMode) {
lua_getglobal(L, "http");
lua_pushnil(L);
lua_setfield(L, -2, "addListener");
lua_pushnil(L);
lua_setfield(L, -2, "removeListener");
lua_pop(L, 1);
if (config.http_enable) {
lua_getglobal(L, "http");
lua_pushnil(L);
lua_setfield(L, -2, "addListener");
lua_pushnil(L);
lua_setfield(L, -2, "removeListener");
lua_pushnil(L);
lua_setfield(L, -2, "websocketServer");
lua_pop(L, 1);
}
lua_pushnil(L);
lua_setglobal(L, "mounter");
lua_pushnil(L);
lua_setglobal(L, "config");
}

// Set default globals
Expand Down
Loading

0 comments on commit 98e4ece

Please sign in to comment.