本帖最后由 flyingbing 于 2015-12-11 14:03 编辑
WRTnode2R 使用的Openwrt 系统里面已经整合了各种 lib ,各种小工具。。
openwrt 系统用起来是挺爽的,但是,可折腾性就没那么高了。
本文做 gameserver 使用的 libwebsockets也在里面集成,不过不是官方的完整版本。
libwebsockets 同时需要依赖 json-c 以及 openssl(这两个库的配置编译不做介绍).
最近做的 游戏服务器gameserver,由于要用到 http 的长连接,需要用到 websockets,所以自己动手移植 libwebsockets 到wrtnode2r。
源码官网在https://libwebsockets.org/index.html
获取libwebsockets源码的途径如下:
You can get the latest sources from git git clone git://git.libwebsockets.org/libwebsockets
or browse the git repo at http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/
libwebsockets is also on github at https://github.com/wARMcat/libwebsockets.
本文采用 git clone 方式:
chris@RD0326:~/openwrt-wrtnode-github-master/package/libs$ git clone git://git.libwebsockets.org/libwebsockets
Cloning into 'libwebsockets'...
remote: Counting objects: 6996, done.
remote: Compressing objects: 100% (6523/6523), done.
remote: Total 6996 (delta 5364), reused 587 (delta 452)
Receiving objects: 100% (6996/6996), 4.61 MiB | 92.00 KiB/s, done.
Resolving deltas: 100% (5364/5364), done.
Checking connectivity... done.
查看下载的文件:
chris@RD0326:~/openwrt-wrtnode-github-master/package/libs/libwebsockets$ ls
Android.mk CMakeLists.txt FindLibWebSockets.cmake LICENSE README.build.md scripts
appveyor.yml cross-arm-linux-gnueabihf.cmake lib lws_config.h.in README.coding.md test-server
changelog cross-ming.cmake libwebsockets-api-doc.html lws_config_private.h.in README.md travis_install.sh
cmake cross-openwrt-makefile libwebsockets.spec module.json README.test-apps.md win32port
查看目录结构以及 README.build.md,libwebsockets 采用 cmake 编译方式。
CMake是一个跨平台安装(编译)工具,可以生成适合多种目标平台的编译文件,更多信息请参看http://www.cmake.org。
不过你也可以使用如下 Makefile 完成来自动从远程下载源码文件以及编译等任务:
- #
- # libwebsockets makefile for openwrt
- #
- include $(TOPDIR)/rules.mk
- PKG_NAME:=libwebsockets
- PKG_VERSION:=2014-03-01
- PKG_RELEASE=$(PKG_SOURCE_VERSION)
- PKG_SOURCE_PROTO:=git
- PKG_SOURCE_URL:=https://github.com/warmcat/libwebsockets.git
- PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
- PKG_SOURCE_VERSION:=388dc7d201d8d123841869fb49ec4d94d6dd7f54
- PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
- CMAKE_INSTALL:=1
- include $(INCLUDE_DIR)/package.mk
- include $(INCLUDE_DIR)/cmake.mk
- CMAKE_OPTIONS += -DLWS_OPENSSL_CLIENT_CERTS=/etc/ssl/certs
- CMAKE_OPTIONS += -DLWS_OPENSSL_SUPPORT=ON
- CMAKE_OPTIONS += -DLWS_WITH_SSL=ON
- CMAKE_OPTIONS += -DLWS_WITHOUT_TESTAPPS=$(if $(CONFIG_PACKAGE_libwebsockets-examples),"OFF","ON")
- # for wolfssl, define these in addition to LWS_OPENSSL_SUPPORT and
- # edit package/libs/wolfssl/Makefile to include --enable-opensslextra
- # CMAKE_OPTIONS += -DLWS_USE_WOLFSSL=ON
- # CMAKE_OPTIONS += -DLWS_WOLFSSL_LIBRARIES=$(STAGING_DIR)/usr/lib/libwolfssl.so
- # CMAKE_OPTIONS += -DLWS_WOLFSSL_INCLUDE_DIRS=$(STAGING_DIR)/usr/include
- # for cyassl, define these in addition to LWS_OPENSSL_SUPPORT and
- # edit package/libs/wolfssl/Makefile to include --enable-opensslextra
- # CMAKE_OPTIONS += -DLWS_USE_CYASSL=ON
- # CMAKE_OPTIONS += -DLWS_CYASSL_LIBRARIES=$(STAGING_DIR)/usr/lib/libcyassl.so
- # CMAKE_OPTIONS += -DLWS_CYASSL_INCLUDE_DIRS=$(STAGING_DIR)/usr/include
- # other options worth noting
- # CMAKE_OPTIONS += -DLWS_WITHOUT_EXTENSIONS=ON
- # CMAKE_OPTIONS += -DLWS_WITHOUT_DAEMONIZE=ON
- # CMAKE_OPTIONS += -DLWS_WITHOUT_SERVER=ON
- # CMAKE_OPTIONS += -DLWS_WITHOUT_DEBUG=ON
- define Package/libwebsockets/Default
- SECTION:=libs
- CATEGORY:=Libraries
- TITLE:=libwebsockets
- DEPENDS:=+zlib +libopenssl
- endef
- define Package/libwebsockets
- $(call Package/libwebsockets/Default)
- TITLE+= (libraries)
- endef
- define Package/libwebsockets/description
- libwebsockets
- This package contains libwebsocket libraries
- endef
- define Package/libwebsockets-examples
- $(call Package/libwebsockets/Default)
- DEPENDS:=libwebsockets
- TITLE+= (examples)
- endef
- define Package/libwebsockets-examples/description
- libwebsockets examples
- This package contains libwebsockets examples
- endef
- define Package/libwebsockets/install
- $(INSTALL_DIR) $(1)/usr/lib
- $(CP) $(PKG_INSTALL_DIR)/usr/lib/libwebsockets.so* $(1)/usr/lib/
- endef
- define Package/libwebsockets-examples/install
- $(INSTALL_DIR) $(1)/usr/bin
- # $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/libwebsockets-test-client $(1)/usr/bin/
- # $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/libwebsockets-test-echo $(1)/usr/bin/
- # $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/libwebsockets-test-fraggle $(1)/usr/bin/
- # $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/libwebsockets-test-ping $(1)/usr/bin/
- $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/libwebsockets-test-server $(1)/usr/bin/
- # $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/libwebsockets-test-server-extpoll $(1)/usr/bin/
- $(INSTALL_DIR) $(1)/usr/share/libwebsockets-test-server
- $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/libwebsockets-test-server/favicon.ico $(1)/usr/share/libwebsockets-test-server/
- $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/libwebsockets-test-server/leaf.jpg $(1)/usr/share/libwebsockets-test-server/
- $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/libwebsockets-test-server/libwebsockets.org-logo.png $(1)/usr/share/libwebsockets-test-server/
- $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/libwebsockets-test-server/libwebsockets-test-server.key.pem $(1)/usr/share/libwebsockets-test-server/
- $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/libwebsockets-test-server/libwebsockets-test-server.pem $(1)/usr/share/libwebsockets-test-server/
- $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/libwebsockets-test-server/test.html $(1)/usr/share/libwebsockets-test-server/
- endef
- $(eval $(call BuildPackage,libwebsockets))
- $(eval $(call BuildPackage,libwebsockets-examples))
复制代码
或者参照wrtnode2r 的makefile 如下:
- #
- # Copyright (C) 2014-2015 OpenWrt.org
- #
- # This is free software, licensed under the GNU General Public License v2.
- # See /LICENSE for more information.
- #
- include $(TOPDIR)/rules.mk
- PKG_NAME:=libwebsockets
- PKG_VERSION:=1.3-chrome37-firefox30
- PKG_RELEASE:=1
- PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
- PKG_SOURCE_PROTO:=git
- PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
- PKG_SOURCE_URL:=https://github.com/warmcat/libwebsockets.git
- PKG_SOURCE_VERSION:=v$(PKG_VERSION)
- PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/$(PKG_SOURCE_SUBDIR)
- PKG_LICENSE:=LGPL-2.1+exception
- PKG_LICENSE_FILES:=LICENSE
- CMAKE_INSTALL:=1
- include $(INCLUDE_DIR)/package.mk
- include $(INCLUDE_DIR)/cmake.mk
- CMAKE_OPTIONS += $(if $(CONFIG_PACKAGE_kmod-ipv6),,-DLWS_IPV6=)
- CMAKE_OPTIONS += -DLWS_WITHOUT_TESTAPPS=ON
- # other options worth noting
- # CMAKE_OPTIONS += -DLWS_WITHOUT_EXTENSIONS=ON
- # CMAKE_OPTIONS += -DLWS_WITHOUT_DAEMONIZE=ON
- # CMAKE_OPTIONS += -DLWS_WITHOUT_SERVER=ON
- # CMAKE_OPTIONS += -DLWS_WITHOUT_DEBUG=ON
- define Package/$(PKG_NAME)/Default
- SECTION:=libs
- CATEGORY:=Libraries
- TITLE:=libwebsockets
- DEPENDS:=+zlib
- endef
- define Package/libwebsockets-openssl
- $(call Package/$(PKG_NAME)/Default)
- TITLE += (OpenSSL)
- DEPENDS += +libopenssl
- VARIANT:=openssl
- endef
- define Package/libwebsockets-cyassl
- $(call Package/$(PKG_NAME)/Default)
- TITLE += (CyaSSL)
- DEPENDS += +libcyassl
- VARIANT:=cyassl
- endef
- ifeq ($(BUILD_VARIANT),openssl)
- CMAKE_OPTIONS += -DLWS_OPENSSL_CLIENT_CERTS=/etc/ssl/certs
- CMAKE_OPTIONS += -DLWS_OPENSSL_SUPPORT=ON
- CMAKE_OPTIONS += -DLWS_WITH_SSL=ON
- endif
- ifeq ($(BUILD_VARIANT),cyassl)
- CMAKE_OPTIONS += -DLWS_OPENSSL_CLIENT_CERTS=/etc/ssl/certs
- CMAKE_OPTIONS += -DLWS_OPENSSL_SUPPORT=ON
- CMAKE_OPTIONS += -DLWS_WITH_SSL=ON
- # for cyassl, edit package/libs/cyassl/Makefile to include --enable-opensslextra
- # NOTE: it will compile without it, untested whether it it's needed?!
- CMAKE_OPTIONS += -DLWS_USE_CYASSL=ON
- CMAKE_OPTIONS += -DLWS_CYASSL_LIB=$(STAGING_DIR)/usr/lib/libcyassl.so
- CMAKE_OPTIONS += -DLWS_CYASSL_INCLUDE_DIRS=$(STAGING_DIR)/usr/include
- endif
- define Package/libwebsockets/install
- $(INSTALL_DIR) $(1)/usr/lib
- $(CP) $(PKG_INSTALL_DIR)/usr/lib/libwebsockets.so* $(1)/usr/lib/
- endef
- Package/$(PKG_NAME)-cyassl/install = $(Package/$(PKG_NAME)/install)
- Package/$(PKG_NAME)-openssl/install = $(Package/$(PKG_NAME)/install)
- $(eval $(call BuildPackage,libwebsockets-openssl))
- $(eval $(call BuildPackage,libwebsockets-cyassl))
复制代码
然后执行make menuconfig
----》libraries
编译的时候把 示例程序也编进去,在网页输入 127.0.0.1:7681可以看到如下运行效果
只需要在任何一个浏览器客户端画图案,该例程会将整个画板传输到其他所有客户端,
用这个可以实现类似你画我猜的小游戏。
之后就可以根据自己游戏的逻辑,利用这套例程做自己定制的小型游戏服务器。
0
|
|
|
|