sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.66k stars 1.65k forks source link

Send Binary Data #591

Open anupgupta82 opened 4 years ago

anupgupta82 commented 4 years ago

Hello,

Please let me know how to send binary data using WebSocketSharp. using Unity 2018.4.14f1 and platform is Android and IOS.

shawkes commented 4 years ago

Send accepts a byte[]. On the receiving end you do something like:

private void Ws_OnMessage(object sender, MessageEventArgs e)
        {

            if (e.IsBinary)
            {
              byte[] theMessage = e.rawdata;
             //do stuff with your binary data
            }
        }

Take a look at the example projects if you haven't. It's a pretty easy library to use.

anupgupta82 commented 4 years ago

Hi, thanks for the help. I have seen other Unity Socket.io libraries that make a wrapper of the class Packet to bind packet ID, callback method, message type and data and then then send it to server . is it necessary to wrap such a class with websocketsharp.

shawkes commented 4 years ago

A github etiquette tip: issues are for bugs and feature requests.

Your initial question looked like it might be a feature request or clarifying whether a feature exists.

Your follow-up question is best suited to Unity Answers or Stack Exchange.

First you'll want to look up what a websocket is: https://en.wikipedia.org/wiki/WebSocket This project is simply an implementation of Websockets in C#.

Read the documentation and run through the examples. Plenty of people are running this on unity (including myself) so there are resources out there. Different people will be using it in different ways (maybe some send string data, maybe some use protobufs and send those as binary or W.H.Y). I used it as a replacement for Photon socket server, so my usage matches how they implement messaging.