SocketPlugin
A Figura Addon that allows TCP sockets and more.
Quick challenge
How far can you run before the mobs catch you?
Minecraft check
Confirm your run
Complete the quick check to get your code.
SocketPlugin
VERY WORK-IN-PROGRESS
A figura addon that allows you to open and use TCP sockets.
You can contact me at `ktzukii` on Discord if the mod crashes your game, or doesn't work as intended.
(please attach the logs on the message, if not you're getting a "invalid, read description")
Documentation:
SocketAPI (global `socket`)
So please add host checks on your socket scripts, otherwise your avatar will error on other clients, and will cause unneccesary trouble and confusion.
So please add host checks on your socket scripts, otherwise your avatar will error on other clients, and will cause unneccesary trouble and confusion.
`SocketAPI:newSocket(String host, Number port) -> LuaSocket`
Opens a new socket, will print errors in log or throw vm error.
`SocketAPI:newWebsocket(String host, Number port) -> LuaWebsocket`
Opens a new websocket, will print errors in log or throw vm error.
`SocketAPI:closeAllSocket()` Close all sockets on the current avatar
`SocketAPI:closeAllWebsocket()` Close all websockets on the current avatar
`SocketAPI:closeAll() -> nil`
Closes all sockets and/or websockets on the current avatar
LuaSocket
`LuaSocket:send(String data) -> nil`
Sends data, limited to strings due to how I wrote this mod. (working on changing that)
`LuaSocket:recv() -> string`
Recieves data, also limited to strings. (see above)
`LuaSocket:hasData() -> boolean`
Gets whether or not there is a message to be received, which can then be gotten by `LuaSocket:recv()`
`Luasocket:close() -> nil`
Closes the socket, cleanly.
Sockets close automatically on avatar destroy/reload, but it's not a clean close.
LuaWebsocket
Websockets are known to be buggy at this time.
`LuaWebsocket:send(String data) -> nil`
Sends data, limited to strings due to my understanding of Java.
`LuaWebsocket:recv() -> string`
Recieves data, also limited to strings. (see above)
`LuaWebsocket:hasData() -> boolean`
Gets whether or not there is a message to be received, which can then be gotten by `LuaWebsocket:recv()`
`LuaWebsocket:close(String reason) -> nil`
Closes the socket, cleanly.
Sockets close automatically on avatar destroy/reload, but it's not a clean close.
Examples:
This mod can interact with any TCP socket or websocket, not just python's.
Python & Lua interaction (Socket)
Python server
```python import socket,json,threading
127.0.0.1 restricts it to only your network, 0.0.0.0 lets anyone connect;
only if the port is open (forwarded), otherwise it won't work.
(check https://portforward.com/ for help, this is not related to my mod.)
HOST,PORT="127.0.0.1",9985
recommended to use high port numbers, anything above 1000
def client(sock:socket.socket,addr:tuple[str,int]):
send stuff, the socket can only recieve strings, limitation of the mod right now.
im working on expanding it to send more types, like tables/dicts, etc.
while True: try: raw:bytes=sock.recv() except: pass sock.send(raw)
close the connection after the message, no reason to keep it open
sock.close() break
what i would use if it was a server instead of a echo
while True:
try:
raw:bytes=sock.recv()
data:dict=json.loads(raw.decode())
except: pass
break
def run(): with socket.create_server((HOST,PORT)) as server:
make a loop so that it runs infinitely
while True:
waits until a client connects THEN continues with running past this point
sock,addr=server.accept()
sock : tcp socket
addr : tuple[host,port]
threading.Thread( target=client, args=[sock,addr])
if __name__=="__main__": run() ```
Lua client
```lua if not host:isHost() then -- dont run the rest of the script if the client running the avatar is not the host return end
-- `127.0.0.1` == `localhost` (`localhost` gets autotranslated into `127.0.0.1`) local sock=socket:newSocket("127.0.0.1",9985) -- socket:newSocket returns nil if the client running the avatar is not host; -- so we avoid errors via the above host check. -- socket:newSocket returns nil if the connection failed.
-- check if the socket did open if not sock then print("couldnt connect, check if your server is running") -- and then return to stop errors if it didnt open return end
-- register a tick event (function runs every tick) events.tick:register(function() -- check if the socket has data if sock:hasData() then local raw=sock:recv() -- then print said data print(raw) end end) ```
There are no more examples as of writing.