• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

dotcoo/LuaSocketEvent: Lua Socket Event Asynchronous

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

dotcoo/LuaSocketEvent

开源软件地址(OpenSource Url):

https://github.com/dotcoo/LuaSocketEvent

开源编程语言(OpenSource Language):

C 93.4%

开源软件介绍(OpenSource Introduction):

仅供学习交流,请勿在线上项目使用

Only for learning exchange, do not use online projects

Lua Socket Event

LuaSocketEvent the Lua Socket Event, Asynchronous Socket. support platform for Linux, MacOSX, Windows, IOS, Android, Cocos2d-x.

LuaSocketEvent supports data and message in two formats, data are the original data, the message is 4 bytes unsigned int little endian and message content

Notice

Please implement lua_lock and lua_unlock for thread safe.

Instructions

Server

nc -l 8888

Client

local socketevent = require("socketevent")

sock = socketevent.tcp()

sock:on("connect", function(event)
	print("connect")
end)

sock:on("data", function(event)
	print("data: " .. event.data)
end)

sock:on("close", function(event)
	print("close!")
end)

sock:on("error", function(event)
	print("error: " .. event.error .. ", " .. event.message)
end)

sock:connect("127.0.0.1", 8888)

sock:send("hello server\n")

Run Data Client

lua -i test_data.lua

Client Send Data

> sock:send("hello server\n")

Message Server

printf "\x0c\x00\x00\x00hello client\x0c\x00\x00\x00hello client" | nc -l 8888 | xxd

Message Client

......

sock:on("message", function(event)
	print("data: " .. event.data)
end)

......

sock:sendmessage("hello server")

Build LuaSocketEvent Library

*nix binary

gcc -o socketevent socketevent.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl -lpthread

linux

gcc -fPIC --shared -o socketevent.so socketevent.c -lpthread

macosx

gcc -o socketevent.o -c socketevent.c
gcc -bundle -undefined dynamic_lookup -o socketevent.so socketevent.o

windows

  1. create lua-5.1.4\build.bat file

     cd src
     cl /O2 /W3 /c /DLUA_BUILD_AS_DLL l*.c
     del lua.obj luac.obj
     link /DLL /out:lua53.dll l*.obj
     cl /O2 /W3 /c /DLUA_BUILD_AS_DLL lua.c luac.c
     link /out:lua.exe lua.obj lua53.lib
     del lua.obj
     link /out:luac.exe l*.obj
     cd ..
    
  2. open VS2013 developers tools, run build.bat

  3. copy socketevent.c to lua-5.1.4\src

  4. create socketevent.dll

     cl /O2 /W3 /c /DLUA_BUILD_AS_DLL socketevent.c
     link /DLL /out:socketevent.dll socketevent.obj lua53.lib
    

android

ios

Cococs2d-x

windows cocos-simulator-bin

  1. build source to socketevent.dll

  2. copy socketevent.dll to C:\Program Files (x86)\Cocos\cocos-simulator-bin\win32

  3. main.lua join

     require("socketevent")
    
  4. using socketevent

mac simulator

  1. Xcode open frameworks/runtime-src/proj.ios_mac/LuaTest.xcodeproj

  2. Classes dir create socketevent.h socketevent.c file

  3. lua_module_register.h join

     extern "C" {
     	#include "socketevent.h"
     }
    
  4. lua_module_register function join

     luaopen_socketevent(L);
    
  5. Xcode build for Mac




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap