Coder Social home page Coder Social logo

toroerp / simplmessage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thijse/simplmessage

0.0 2.0 0.0 326 KB

SimplMessage is build on top of the SimplSockets socket library and facilitates sending and receiving objects of any class type. SimplSockets is a socket library that provides highly efficient, scalable, simple socket communication that allows you to send, receive and reply with binary messages. It also comes with auto-discovery and auto-connection functionality

License: MIT License

C# 100.00%

simplmessage's Introduction

logo

SimplMessage is build on top of SimplSocket and facilitates sending and receiving objects of any class type. It also comes with auto-discovery and auto-connection functionality

SimplSockets is a socket library that provides highly efficient, scalable, simple socket communication that allows you to send, receive and reply with binary messages.

Quickstart

To give a sense of what the library does, let's look at a basic example of sending an object from a client to the server

using System;
using SimplMessage;
using System.Net;

namespace SimplSocketsClient
{
    public class ClientSendMessage
    {
        public void Start()
        {
            CreateServer();
            CreateClient();
            Console.ReadLine();            
        }

        // Define a class to be send
        public class ClassA
        {
            public int    VarInt;
            public double VarDouble;
        }
   
        void CreateServer()
        {
            // Create the server
            var server = new SimplMessageServer();

            // Create a callback for received data of type classA
            // (you could also implement this as a lambda function)
            server.AddCallBack<ClassA>(ServerReceivedClassACallback);            

            // Start listening for client connections on loopback end point
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
        }

        void CreateClient()
        {
            // Create the client
            var client = new SimplMessageClient();

            // Automatically discover the server and connect
            client.AutoConnect();

            // Wait until the connection is actually made
            client.WaitForConnection();

            // Create an object to send
            var objectToSend  = new ClassA() { VarInt = 2, VarDouble = 2.5 };

            // Send the object
            client.Send(objectToSend);
            
            // Done!
        }

        // Function is called when an object of type ClassA is received 
        private void ServerReceivedClassACallback(ReceivedMessage receivedMessage)
        {
            // get data from received message
            var receivedObject = receivedMessage.GetContent<ClassA>();

            // Notify that the server received data
            Console.WriteLine($"Server received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");
        }
    }
}

That's it! The Wiki goes into more depth on different topics:

Credits

This library is forked from SimplSockets (MIT) by David Haney. It has been significantly refactored, optimized and extended, but a lot, if not most, of the code is based on that library.

The serialization and deserialization of objects is done using msgpack-cli (Apache) by Yusuke Fujiwara.

The server discovery code is done using Beacon (MIT) by Rico Huijbers

simplmessage's People

Contributors

haneytron avatar thijse avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.