This commit is contained in:
SongWei 2020-02-23 12:10:08 +11:00
parent f3383f2098
commit aeff6a1d2a
3 changed files with 54 additions and 32 deletions

View File

@ -1,25 +1,25 @@
[] spawn { [] spawn {
//if (!hasInterface) exitWith {}; //if (!hasInterface) exitWith {};
if (!isServer) exitWith {}; if (!isServer) exitWith {};
//if (!isDedicated) exitWith {}; //if (!isDedicated) exitWith {};
addMissionEventHandler ["ExtensionCallback", { addMissionEventHandler ["ExtensionCallback", {
params ["_name", "_function", "_data"]; params ["_name", "_function", "_data"];
if (_name isEqualTo "a3web") then if (_name isEqualTo "a3web") then
{ {
diag_log format ["ExtensionCallback %1, %2, %3", _name, _function, _data]; diag_log format ["ExtensionCallback %1, %2, %3", _name, _function, _data];
}; };
}]; }];
waitUntil {time > 0}; waitUntil {time > 0};
a3web_interval = 1;
while {true} do { a3web_interval = 1;
sleep a3web_interval;
private _units = allUnits apply { while {true} do {
[netid _x, isPlayer _x, name _x, str (side _x), getPos _x]; sleep a3web_interval;
}; private _units = allUnits apply {
[netid _x, isPlayer _x, name _x, str (side _x), getPos _x];
};
"liba3web" callExtension ["http:post:/units-info", [_units]]; "liba3web" callExtension ["http:post:/units-info", [_units]];
}; };
}; };

View File

@ -16,11 +16,14 @@ package main
import "C" import "C"
import ( import (
"os"
"fmt" "fmt"
"log" "log"
"strings" "strings"
"unsafe" "unsafe"
"net/http" "net/http"
"encoding/json"
"io/ioutil"
) )
var cb C.callbackProc var cb C.callbackProc
@ -45,6 +48,19 @@ func RVExtensionVersion(output *C.char, outputsize C.size_t) {
defer C.free(unsafe.Pointer(version)) defer C.free(unsafe.Pointer(version))
var size = C.min(C.strlen(version)+1, outputsize-1) var size = C.min(C.strlen(version)+1, outputsize-1)
C.strncpy(output, version, size) C.strncpy(output, version, size)
// Load config
const configPath string = "~/.a3web.conf"
type Config struct {
serverURL string `json:"serverURL"`
}
var config Config
if _, err := os.Stat(configPath); err == nil {
configFile, _ := ioutil.ReadFile(configPath)
json.Unmarshal(configFile, &config)
serverURL = config.serverURL
}
} }
// RVExtensionArgs STRING callExtension ARRAY // RVExtensionArgs STRING callExtension ARRAY
@ -56,8 +72,8 @@ func RVExtensionArgs(output *C.char, outputsize C.size_t, function *C.char, argv
out = append(out, C.GoString(*argv)) out = append(out, C.GoString(*argv))
argv = (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + offset)) argv = (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + offset))
} }
go handleArgs(C.GoString(function), argc, out) go handleArgs(C.GoString(function), out)
} }
// RVExtension STRING callExtension STRING // RVExtension STRING callExtension STRING
@ -71,14 +87,20 @@ func RVExtension(output *C.char, outputsize C.size_t, function *C.char) {
func main() {} func main() {}
func handleArgs(function string, argc C.int, argv []string) { func handleArgs(function string, argv []string) {
fns := strings.Split(strings.ToLower(function), ":") switch dispatcher := "function"
if len(fns) == 3 && fns[0] == "http" && fns[1] == "post" { dispatcher {
resp, err := http.Post(serverURL + fns[2], "", strings.NewReader(argv[0])) case "upload_pos":
if err != nil { uploadUnitsInfo(argv[0])
log.Printf("Error Sending HTTP Request: %s", err)
return
}
resp.Body.Close()
} }
} }
func uploadUnitsInfo(body string) {
var endpoint string = "/units-info"
resp, err := http.Post(serverURL + endpoint, "", strings.NewReader(body))
if err != nil {
log.Printf("Error Sending HTTP Request: %s", err)
return
}
resp.Body.Close()
}

View File

@ -82,8 +82,8 @@
(defparameter +dispatch-table+ (defparameter +dispatch-table+
`(("^/$" ,#'route-ok) `(("^/$" ,#'route-ok)
("^/post-arma3-info$" ,#'route-post-arma3-info) ("^/positional-info$" ,#'route-post-arma3-info)
("^/units-pos$" ,#'route-units-pos) ("^/units-info$" ,#'route-units-pos)
(,(concatenate 'string "^" +static-prefix+ ".*$") ,#'serve-static-file) (,(concatenate 'string "^" +static-prefix+ ".*$") ,#'serve-static-file)
(nil ,#'route-not-found))) (nil ,#'route-not-found)))