Developing an extension for Oxide?

I want to use Websocket in plugin , but it's unable used ,Can I develop a Extensions(DLL)?how to develop it, and where can I find "HollWorld" program

Thank you, I have been read it,but I don't know what's the Enter Function(like "Main()" Function of Desktop apploction) on Oxide Extensions DLL, or what is OnServerInitialized  API in DLL?

cuilu007

Thank you, I have been read it,but I don't know what's the Enter Function(like "Main()" Function of Desktop apploction) on Oxide Extensions DLL, or what is OnServerInitialized  API in DLL?

Hooks are not called within the extension class. Extension constructor is called first, and after that there are virtual methods you can override which names are self descriptive

My Code:

using Newtonsoft.Json;
using System;
using System.Timers;
using Oxide.Core;
using Oxide.Core.Extensions;
using Oxide.Core.Libraries;
using Oxide.Ext.Mhzl.com;
namespace Oxide.Ext.Mhzl
{
    public class MhzlExtension:Extension
    {
        public MhzlExtension(ExtensionManager manager): base(manager){}
        public override string Name { get { return "Mhzl"; } }
        public override VersionNumber Version { get { return new VersionNumber(1, 0, OxideMod.Version.Patch); } }
        public override string Author { get { return "JiaoYang"; } }
        public override void Load()
        {

        }
        public override void LoadPluginWatchers(string plugindir)
        {
        }

        public override void OnModLoad()
        {
            var Main = new main();
            Manager.RegisterLibrary(nameof(Main), Main);
        }

        public override void OnShutdown()
        { 
        }

    }
}
​
it was working,But I don't know when OnModLoad API  was running,There are some Function of Websocket in mian,I can see some connect log in my websocket server,and I don't  know how long the function which named OnModLoad was runing,I want to run function forever,Becase there are some websocket function in this function,it keep connect my websocket server.
cuilu007

My Code:

using Newtonsoft.Json;
using System;
using System.Timers;
using Oxide.Core;
using Oxide.Core.Extensions;
using Oxide.Core.Libraries;
using Oxide.Ext.Mhzl.com;
namespace Oxide.Ext.Mhzl
{
    public class MhzlExtension:Extension
    {
        public MhzlExtension(ExtensionManager manager): base(manager){}
        public override string Name { get { return "Mhzl"; } }
        public override VersionNumber Version { get { return new VersionNumber(1, 0, OxideMod.Version.Patch); } }
        public override string Author { get { return "JiaoYang"; } }
        public override void Load()
        {

        }
        public override void LoadPluginWatchers(string plugindir)
        {
        }

        public override void OnModLoad()
        {
            var Main = new main();
            Manager.RegisterLibrary(nameof(Main), Main);
        }

        public override void OnShutdown()
        { 
        }

    }
}
​
it was working,But I don't know when OnModLoad API  was running,There are some Function of Websocket in mian,I can see some connect log in my websocket server,and I don't  know how long the function which named OnModLoad was runing,I want to run function forever,Becase there are some websocket function in this function,it keep connect my websocket server.

Which WS library do you use? Most of them provide an option to keep connection open as long as the object is not gc'ed, so you don't need a 'forever running function' whatever that means. If you still need to send some kind of keepalive requests with certain interval - you can use Timer library, OnTick method, or create a custom Component with either InvokeRepeating or lifecycle method in it.

i9VUU5ecDQLoWEq.png 2CHEVSKII

Which WS library do you use? Most of them provide an option to keep connection open as long as the object is not gc'ed, so you don't need a 'forever running function' whatever that means. If you still need to send some kind of keepalive requests with certain interval - you can use Timer library, OnTick method, or create a custom Component with either InvokeRepeating or lifecycle method in it.

let me have a try,but I dont know how to call DLL function in Plugin(oxide\plugins\*.cs)

cuilu007

let me have a try,but I dont know how to call DLL function in Plugin(oxide\plugins\*.cs)

You can call it directly if you make the dll a hard dependency of the plugin (essentially including // Requires: comment at the top of plugin code), then include it's namespace and call methods as usual, why would you need that? You can embed plugin directly into your extension

Ok,thank you,I' have a new question,how to get exception in DLL,"Try-catch" does not seem working:

        private void onmessage(object sender, MessageEventArgs e)
        {
        }
        private void onerror(object sender, ErrorEventArgs e)
        {
            if (sender is WebSocket ws)
            {
                ws.OnMessage -= onmessage;
                ws.OnError -= onerror;
                ws.OnOpen -= onopen;
                ws.CloseAsync();
            }
            ws = null;
            canSend = false;
            Interface.Oxide.LogWarning("[MHZL]" + "He Fu Wu Qi De Lian Jie Yi Jing Duan Kai");
            reconnTimer.Start();
        }
        private void onopen(object sender, EventArgs e)
        {
            reconnTimer.Stop();
            canSend = true;
        }
        private void conn()
        {
            reconnTimer.Stop();
            if (!(ws is null))
            {
                ws.OnMessage -= onmessage;
                ws.OnError -= onerror;
                ws.OnOpen -= onopen;
                ws = null;
            }
            ws = new WebSocket(url);
            ws.OnMessage += onmessage;
            ws.OnError += onerror;
            ws.OnOpen += onopen;
            ws.ConnectAsync();
        }​

There is a exception like "2021/10/15 19:59:12|Fatal|WebSocket.<startReceiving>b__170_2|System.IO.IOException: Unable to read data from the transport connection:" in CMD When my websocket server was closed.exception is not disappear when I used try catch.How to catch this exception

cuilu007

Ok,thank you,I' have a new question,how to get exception in DLL,"Try-catch" does not seem working:

        private void onmessage(object sender, MessageEventArgs e)
        {
        }
        private void onerror(object sender, ErrorEventArgs e)
        {
            if (sender is WebSocket ws)
            {
                ws.OnMessage -= onmessage;
                ws.OnError -= onerror;
                ws.OnOpen -= onopen;
                ws.CloseAsync();
            }
            ws = null;
            canSend = false;
            Interface.Oxide.LogWarning("[MHZL]" + "He Fu Wu Qi De Lian Jie Yi Jing Duan Kai");
            reconnTimer.Start();
        }
        private void onopen(object sender, EventArgs e)
        {
            reconnTimer.Stop();
            canSend = true;
        }
        private void conn()
        {
            reconnTimer.Stop();
            if (!(ws is null))
            {
                ws.OnMessage -= onmessage;
                ws.OnError -= onerror;
                ws.OnOpen -= onopen;
                ws = null;
            }
            ws = new WebSocket(url);
            ws.OnMessage += onmessage;
            ws.OnError += onerror;
            ws.OnOpen += onopen;
            ws.ConnectAsync();
        }​

There is a exception like "2021/10/15 19:59:12|Fatal|WebSocket.<startReceiving>b__170_2|System.IO.IOException: Unable to read data from the transport connection:" in CMD When my websocket server was closed.exception is not disappear when I used try catch.How to catch this exception

I don't see any try-catch clauses in your code. Remember that if you are calling async method, it's exceptions won't be catched unless you await them