LuaSocket
Posted: Sat Apr 27, 2024 2:43 pm
Is it possible to add LuaSocket module to the Moho?
Code: Select all
package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/scriptresources/?.lua;"
package.cpath = package.cpath .. ";" .. moho:UserAppDir() .. "/scripts/scriptresources/?.dll;"
Code: Select all
local socket = require "socket"
local connection = socket.tcp()
connection:settimeout(1000)
local result = connection:connect("ifconfig.co", 80)
connection:close()
print("result: ", result)
Code: Select all
local http = require("socket.http")
local response = http.request("https://ifconfig.co/json")
print("response: ", response)
Sam, would you please tell me where (in which file) , I can set those path and cpath settings?SimplSam wrote: ↑Sun Apr 28, 2024 9:39 pmCode: Select all
package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/scriptresources/?.lua;" package.cpath = package.cpath .. ";" .. moho:UserAppDir() .. "/scripts/scriptresources/?.dll;"
Thank you very much for sharing.SimplSam wrote: ↑Sun Apr 28, 2024 9:39 pm This is from a previous discussions:
I did find an out-of-the-box Windows binary 5.4 LuaSocket implementation that worked: https://github.com/alain-riedinger/luasocket. Release: https://github.com/alain-riedinger/luas ... /3.0-5.4.3
This just requires updates to path and cpath to point at the installed-to directory (scriptresources in my case), and then use.
...Code: Select all
package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/scriptresources/?.lua;" package.cpath = package.cpath .. ";" .. moho:UserAppDir() .. "/scripts/scriptresources/?.dll;"
Connection Test:...Code: Select all
local socket = require "socket" local connection = socket.tcp() connection:settimeout(1000) local result = connection:connect("ifconfig.co", 80) connection:close() print("result: ", result)
Simple HTTP Request *:* Note: There is a slight issue with this shipped LuaSockets build (which is 3 years old - and contains an old compatibility buglet) when using HTTP and Lua 5.3/5.4. (see: https://github.com/lunarmodules/luasocket/issues/331). The quick fix is to: Update/Replace all receive() calls in http.lua and tp.lua with receive("*l")Code: Select all
local http = require("socket.http") local response = http.request("https://ifconfig.co/json") print("response: ", response)
Through my search, I found a possible explanation: it seems the Mac version of Moho needs to be built with the “LUA_USE_DLOPEN” macro added during its compilation.error loading module 'socket.core' from file '/Users/xxx/moho/user_define_content/Moho Pro/Script/ScriptResources/socket/core.dylib': dynamic libraries not enabled; check your Lua installation