gluawebsocket

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 4 Imported by: 0

README

A websocket lib for GopherLua

A websocket client for the GopherLua VM, based on gorilla/websocket

Installation

go get github.com/Root-lee/gluawebsocket

Using

Loading Modules
import (
	"github.com/Root-lee/gluawebsocket"
)

// Bring up a GopherLua VM
L := lua.NewState()
defer L.Close()

// Preload websocket modules
gluawebsocket.Preload(L)
Usage In lua
local ws = require("websocket")

local c, _, err = ws.dial("ws://localhost:8080/echo", {})
if err then
    print(err)
    return
end

err = c:write_text("hello")
if err then
    print(err)
    return
end

local msg_type, msg
msg_type, msg, err = c:read()
if err then
    print(err)
    return
end

print(string.format("receive a message, msg_type: %d, msg: %s", msg_type, msg))

err = c:ping("ping")
if err then
    print(err)
    return
end

err = c:close(1000, "")
if err then
    print(err)
    return
end

Testing

Unit Test
$ go test github.com/Root-lee/gluawebsocket...
PASS
coverage: 89.4% of statements
ok  	gluawebsocket	0.389s
Manual Test

You can use this Websocket Echo Server to start a websocket server

Then you can use this demo to test your lua file

package main

import (
	"github.com/Root-lee/gluawebsocket"
	lua "github.com/yuin/gopher-lua"
)

func main() {
	L := lua.NewState()
	gluawebsocket.Preload(L)
	defer L.Close()

	if err := L.DoFile("test.lua"); err != nil {
		panic(err)
	}
}

You can refer to this lua script to write your own lua script

License

MIT

Documentation

Index

Constants

View Source
const (
	WRITE_TIMEOUT           = time.Second
	WEBSOCKET_CONN_TYPENAME = "websocket_conn_typename"
)

Variables

This section is empty.

Functions

func Loader

func Loader(L *lua.LState) int

func Preload

func Preload(L *lua.LState)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL