Integrating LuaSockets in cocos2dx-lua

Hi,

Is it possible to integrate LuaSockets [ http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0-alpha/home.html#down ] in my cocos2dx project?
I need it to run on Android and iOS. How can I do that?

To be honest I have no clue how to deal with such things [implementing external libraries into cocos2dx-lua project] to maintain ability to build for Android and iOS.
A little description would be awesome. Something I could use in future.

open: https://github.com/dualface/cocos2d-x/tree/master/lua/exts

steps:

  1. copy below files to your project /libs/luaexts/:
    * luasocket/*
    * luasocketscropts.c/.h
    * lualoadexts.c/.h

  2. modify lualoadexts.c/.h:
    * remove #include “lua_cjson.h”
    * remove #include “llthreads.h”
    * remove “cjson”, “llthreads” from luax_exts[]

  3. modify libs/lua/cocos2dx_support/LuaEngineImpl.cpp
    * add #include “lualoadexts.h”

    extern “C” {
    #include “lualoadexts.h”
    }

  • append below code to CCLuaScriptModule::CCLuaScriptModule() last line:
    <pre>
    luax_loadexts(d_state);
    </pre>

Sir, you’re a star!
Thanks a lot for sharing this!

I do get an error however:

Ld /Users/krystian/Library/Caches/appCode10/DerivedData/ogsClient-101d8890/Build/Products/Debug-iphonesimulator/ogsClient.app/ogsClient normal i386
    cd /Users/krystian/ogsClient
    setenv MACOSX_DEPLOYMENT_TARGET 10.6
    setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/krystian/Library/Caches/appCode10/DerivedData/ogsClient-101d8890/Build/Products/Debug-iphonesimulator -F/Users/krystian/Library/Caches/appCode10/DerivedData/ogsClient-101d8890/Build/Products/Debug-iphonesimulator -filelist /Users/krystian/Library/Caches/appCode10/DerivedData/ogsClient-101d8890/Build/Intermediates/ogsClient.build/Debug-iphonesimulator/ogsClient.build/Objects-normal/i386/ogsClient.LinkFileList -mmacosx-version-min=10.6 -ObjC -all_load -Xlinker -objc_abi_version -Xlinker 2 -framework CoreGraphics -framework Foundation -framework OpenGLES -framework QuartzCore -framework UIKit -framework AudioToolbox -framework OpenAL -lz -framework AVFoundation "-lcocos2d libraries" -lxml2 -o /Users/krystian/Library/Caches/appCode10/DerivedData/ogsClient-101d8890/Build/Products/Debug-iphonesimulator/ogsClient.app/ogsClient
Undefined symbols for architecture i386:
  "luax_loadexts(lua_State*)", referenced from:
      CCLuaScriptModule::CCLuaScriptModule()in LuaEngineImpl.o
      CCLuaScriptModule::CCLuaScriptModule()in LuaEngineImpl.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

I’m sure it’s all about some configuration but since I’ve never added additional libraries to a project in c++ I have no clue what am I doing wrong.
All the steps you mentioned in your post are done. In AppCode [I use that for iOS development] I can see everything just fine. All the imports etc.

EDIT:
and one more thing. Do I need anything to make this work under android as well?

:slight_smile:

extern "C" {
#include "lualoadexts.h"
}

You’re the man!
Thanks a lot!

thank you ….so much…

This is a little bit old now and the luaext stuff has been removed from the repository but I would like to be able to integrate luasockets and I’m getting errors.

I got the old luaext/luasocket files from the repository history and when attempting a connection I get an error about socket/ltn12.lua line 41 no arg variable. This is because I am using luajit which doesn’t allow usage of the global arg variable. I could fix the source but I’m not sure how the bytecode strings were generated for luasocket. In luasocketscripts.c there are some arrays set up with strings for setting up some of the core luasocket functions, I think I need to be able to somehow fix the luasocket source and then create those strings (in this case B6 for luaopen_socket_ltn12)

I was able to alter the luasockets implementation slightly to get things working with luajit. The main things I changed are:

  1. Comment out the line for “luaopen_socket_ltn12” in lualoadexts.c:
    // {"ltn12", luaopen_socket_ltn12},
  2. Put the lua file ltn12.lua into your base lua script directory
  3. Change the ltn12.lua file, luajit does not support the global “arg” variable so you should change the filter.chain(...) function to look like this:

local arg={…}
local n = #arg

The old luasocket way is to use table.getn(arg), not supported by luajit

It worked!