Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 1.93 KB

README.md

File metadata and controls

79 lines (62 loc) · 1.93 KB

jSock Logo

jSock

My C# WebSocket Server

How to use jSock?

jSock is avaliable on NuGet and can be used in any .NET Core project.

NuGet Package

dotnet add package jSock
Install-Package jSock

Example of a jSock Server

using jSock;

class RTC : jSockServer
{
    public RTC(string address, int port) : base(address, port) {}

    //Runs when a client connects to the server.
    public override void OnConnect(int clientID)
    {
        Console.WriteLine("A client has connected!");
    }

    //Runs when a client disconnects from the server.
    public override void OnDisconnect()
    {
        Console.WriteLine("A client has disconnected!");
    }

    //Runs when a client sends a message to the server.
    public override void OnRecieve(int clientID, string text)
    {
        //Display the message.
        Console.WriteLine("A message from a client! " + text);

        //Send the message back to the client that sent it.
        Reply(clientID, "Thanks for your message!");

        //Send the message to all clients except the one that sent it.
        Broadcast("Hey guys, some client sent me a message!", clientID);
    }
}

class Program
{
    static void Main()
    {
        //Create a new jSock server instance and start it.
        RTC server = new RTC("127.0.0.1", 8080);
        server.Start();
    }
}

JavaScript Client

const jsocket = new WebSocket("ws://127.0.0.1:8080");

jsocket.onopen = function(event) {
    console.log("Connected to jSock Server!");

    console.log("Sending message to jSock Server...");
    jsocket.send("Hello, jSock Server!");
};

jsocket.onmessage = function(event) {
    console.log("Message from jSock Server: " + event.data);
};

jsocket.onerror = function(event) {
    console.log("Error connecting to the jSock Server.");
};