#Web sockets

1 messages · Page 1 of 1 (latest)

turbid kite
#

Here is the thread

#

hi

#

how about webgl support

#

See if you can get access to that class and use it.

#

I don't have unity installed on my machine rn lol

#

okay

#

is this good

#
using System.Net
using UnityEngine;
using WebSocketSharp;


public sealed class ClientWebSocket : System.Net.WebSockets.WebSocket
{
    public class WsClient : MonoBehaviour
    {
        WebSocket ws;
        private void Start()
        {
            ws = new WebSocket("wss://WebSocket.nitrospeednitro.repl.co");
            ws.Connect();
            ws.OnMessage += (sender, e) =>
            {
                Debug.Log("Message Received from " + ((WebSocket)sender).Url + ", Data : " + e.Data);
            };
        }
        private void Update()
        {
            if (ws == null)
            {
                return;
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                ws.Send("Hello");
            }
        }
    }
}
#

Um what you would want to do...

new System.Net.WebSockets.ClientWebSocket()
#

Then call ConnectAsync

#

huh

#

can you add it to the main code?

#

like i dont rly understand new System.Net.WebSockets.ClientWebSocket()

#
// At the top
using System.Net.WebSockets;
// In Start()
var client = new ClientWebSocket();
await client.ConnectAsync(/**uri**/);
#

What is your experience with C#?

turbid kite
#

I'm more a 3d designer & nodejs expert

#

@turbid kite

#

wow

#

is this you

#

haha yeahh

#

That's me lolol

#

omg

#

do it work for webgl to?

#

I don't know

#

hmm

#

It should since I'm using .net plain

#

.net is cross platform and can handle wasm

#

But i'm not familiar with WebGL and don't know if Unity can use System.Net assembly

#

wich one?

#

uh .net framework

#

if it works for webgl your a legend

#

Tell me if this code runs, just to make sure everything is available

#
using UnityEngine;
using WebSocketSharp;
using System.Net.WebSockets;

public class WsClient : MonoBehaviour
{
    WebSocket ws;
    private async void Start()
    {
        var client = new ClientWebSocket();
        await client.ConnectAsync(new Uri("wss://WebSocket.nitrospeednitro.repl.co"), CancellationToken.None);
        ws = new WebSocket("wss://WebSocket.nitrospeednitro.repl.co");
        ws.Connect();
        ws.OnMessage += (sender, e) =>
        {
            Debug.Log("Message Received from " + ((WebSocket)sender).Url + ", Data : " + e.Data);
        };
    }
    private void Update()
    {
        if (ws == null)
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ws.Send("Hello");
        }
    }
}
#

I'm gonna connect again and try to send data

#

ok

turbid kite
#

hEY THIS IS GREAT!

#

It at least is available

turbid kite
#

Yeah we can fix that easily.

#

hmm

#

I sent empty data haah

#

o

#

cool

turbid kite
#

then i can try the WebGl

#

Well we'll just remove the WebSocketCsharp references and replace with System.Net.WebSockets references

#

Btw I sent data

#

emojis,

#

Haha yep this is fun lol

#

Idk just random bytes

#

o

#

hmm

#

0x99 and 0xd5

#

woah

#

i also made buffer to string

#

so if you do text

#

Yep I can do binary or text

#

nice

#

can i delete WebSocketSharp?

#

Yes but we'll have to replace the references with System.Net.WebSockets. Copy your code as is just in case this doesn't work or you're not using source control

#

huh

#

can you make a simple working one script?

#

Yeah sure let me work on it bud

#

Working on it, sent a friend request

#

Anyways it's so hard to create websocket for webgl

#

like

#

unity => webgl + websocket

turbid kite
#

trying to add websocket to my game so later i can add createrplayer, movement, jump... in websocket data

#

Yep

#

hmm if you want u can become developer in my game but idk if you have time for that so yea

#

I don't have time but I have a github we can follow each other

#

I'm gonna spam the server in a sec, just screenshot what it looks like lol

#

sure

#

Ok

#

hmm i see your good in c#

#

let me login

#

sec

#

Thanks lol

#

I'm spamming rn

#

woah

#

i see

#

wtf

#

so fast

#

Also I have your code

#

Alright I have a txt file I'll upload it and you just rename it and replace the file

#

Make sure you copy the old file

#

NiCe!

turbid kite
#

See if that works

#

Again not sure if it'll work but if it works for windows we got at least some success

#

Um delete your other file

#

Wait

#

Just copy the text in the file and paste it in WsClient.cs

#

Delete abc.cs If there is nothing important there

#

done

#

oops lol hold on

#

Add using System.Threading after line 3

#

using System.Threading; to be exact

#

2 errors left 😮

#

Add using System; to the top as well

#

That should take care of the assembly references missing

#

ok no errors

#

Woop woop

#

ok

#

it connect + say one time hello

#

thats good right?

#

It says hello when you hit the space key

#

Like you wanted it to

#

yes

#

Uh hold on

#

Can you show the line that is on

#

Btw does it send any data back?

#

I want to test out if I can receive data

turbid kite
turbid kite
#

Ok perfect

#

And tell me what line 17 is plz

#

await client.ReceiveAsync(seg, CancellationToken.None);

#

Alright thanks

#

testing on your server hold on

#

Give me a sec I just need to edit something

#

What length of response would be plenty? I just need a buffer large enough. Is 100 good?

#

Heyyyy

#

Hahahahaah

turbid kite
#

Yup

#

o

#

but

#

Alright here is your new file

#
using System;
using UnityEngine;
using System.Text;
using System.Net.WebSockets;
using System.Threading;

public class WsClient : MonoBehaviour
{
    ClientWebSocket client;
    private async void Start()
    {
        client = new ClientWebSocket();
        await client.ConnectAsync(new Uri("wss://WebSocket.nitrospeednitro.repl.co"), CancellationToken.None);
        ArraySegment<byte> seg = new();
        while (client.State == WebSocketState.Open)
        {
            await client.ReceiveAsync(seg, CancellationToken.None);
            Debug.Log("Message Received: " + Encoding.ASCII.GetString(seg));
        }
    }
    private async void Update()
    {
        if (client.State == WebSocketState.Closed)
        {
            Debug.Log(client.CloseStatusDescription);
            return;
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            await client.SendAsync(Encoding.ASCII.GetBytes("PlayerAction: Jump"), WebSocketMessageType.Text, true, CancellationToken.None);
        }
    }
}
``` can you add 'ws on connection' it send msg createplayer
#

Sure!

#

Just send a message like in the update method right after connectasync is called in start

#

let me try

#

Alright how did it go

#

Sorry I didn't upload a proper file last time

#

yeah it worked

#

its converting to webgl rn

#

Awesome saucesome

#

PizzaSaucesome

#

and lets seei f it works on browser ....

#

Hopefully....

#

ye

#

ok i have news

#

...

#

Did it work

#

😳

#

uh did it work

#

nah joke still loading lol

#

HAHA

#

read your logs

#

anyways

#

do u know how i get full js, html, css?

#

with webgl

#

read your logs on replit

#

lol

#

i see

#

hahah

#

well

#

its pretty nice to see how you can send msgs to my server xd

#

Uh Idk but you can probably embed it. I've seen sites do it and it can't be too hard

#

haha yep

#

im now trying nodejs localy

#

sec

#

good

#

Having fun with your server lol

#

oh no

#

wut

#

game works but websocket not

#

ugh

#

Is there more log

#

More error output

#

oh wait

#

ok waiting

#

if i jump i get this error

#

paste the whole response

#

I have no idea

#

i guess it didn't work

#

something wrong with compiling?

#

Looks like you didn't save api compatibility level

turbid kite
#

Ctrl+S

#

where

#

The asterisk says you didn't save. Don't think that'll fix

turbid kite
#

so i didn save the game?

#

Didn't save that setting

#

don't think that'll fix it just shooting around

turbid kite
#

says works for webgl

#

Well I tried sorry

#

ok np

#

thanks btw

#

but u can help me with fignet

#

idk haven't installed unity lol

#

k

#

hmm

#

they say i need System.Net.Sockets.

#

@turbid kite

#

Who says that

#

Oh the forum

#

Marco

#

yes

#

u think y can help with that? pls

#

Yeah maybe

#

Well if I use system.net.sockets instead of system.net.websockets maybe I'll change some stuff up

#

what is the ip and port of the socket server

#

wss://WebSocket.nitrospeednitro.repl.co

#

wss://WebSocket.nitrospeednitro.repl.co:8080

#

Hahaha

#

how about i add it on browser? in js

#

Working on stuff hold on

#

did you get anything in logs

#

nope didn't work

#

nope

#

can't use it sorry you'll have to try the unity package

#

did my solution work for pc though?

turbid kite
#

run in unity and see if it works in unity before building

turbid kite
#

No open up unity and hit the play button lol. see if it works IN unity

turbid kite
#

the script you made?

#

Yeahhh

#

yes it worked

#

but not in webgl

#

Oh cool it worked

#

I'm just happy it worked in Unity lol

#

yes

#

but no webgl

#

maybe there's a way to debug it

#

I have no idea

#

h

#

You can call javascript using interopservices

#

yeah i aleardy try that but didn work

#

and i dont know the protocol

#

let me try

#

sec

#

Also

#

do you want be developer?

#

for my game like you help with things

#

No sorry. I have stuff I do and can't really devote the time. I spent 3 and a half years with game dev as my main pastime and I've since moved on.

#

alright no problem

#

but

#

how about i dont compile the websocket client?

#

should it work then?

#

I don't know how you wouldn't compile it. plus compiling has a reason, but go for it

#

i mean compile the script

#

but not with unity

#
WebSocketInit("wss://WebSocket.nitrospeednitro.repl.co", "mygameprotocol");
``` what protocol
#

Hey check if you got logs

#

Sending stuff rn

#

dont see

#

anything

#

Ok nvm

#

Here is my .net code if you ever want it:

using System.Text;
using System.Net.WebSockets;

using var client = new ClientWebSocket();
await client.ConnectAsync(new Uri("wss://WebSocket.nitrospeednitro.repl.co"), CancellationToken.None);
await client.SendAsync(Encoding.ASCII.GetBytes("Setting up connection"), WebSocketMessageType.Text, true, CancellationToken.None);
ArraySegment<byte> seg = new(new byte[100]);
while (client.State == WebSocketState.Open)
{
    await client.ReceiveAsync(seg, CancellationToken.None);
    Console.WriteLine(Encoding.ASCII.GetString(seg));
    var msg = Console.ReadLine()!;
    await client.SendAsync(Encoding.ASCII.GetBytes(msg), WebSocketMessageType.Text, true, CancellationToken.None);
}
#

System.Net.WebSockets no work for webgl

#

but

#

alright

#
using UnityEngine;
using System.Runtime.InteropServices;

public class WsClient : MonoBehaviour
{

    [DllImport("__Internal")]
    private static extern void WebSocketInit(string url, string protocol);

    [DllImport("__Internal")]
    private static extern void WebSocketSend(string message);

    void Start()
    {
        WebSocketInit("wss://WebSocket.nitrospeednitro.repl.co", "protocol one");
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            WebSocketSend("test");
        }
    }
}
#

idk sorry I couldn't help

#

hmm okay

#

can u create something wit hthat?

#

That's http not wss

#

What do you want to do with wss anyway?

#

Cuz you can definitely do http and I've used it before

#

i want a websocket for browser

#

like a multiplayer game

#

What sorta request/response cycle do you want

#

like this

#

you just want to send one message and get one response back each time right?

#

you can use http for that

#

yeah http

#

unless you're sending a response to multiple people and want to keep the connection open like a pool

#

then you may want wss or even rpc

#

RPC is great but I spent weeks trying to get it to work in unity and never did

#

especially gRPC

turbid kite
#

wss !

#

U know how?

#

no I don't

#

I just tried that with you for the last couple hours

#

bruh

#

hmm

#

How about i add websocket client in JavaScript in a js file,

#

sounds good

#

oh

#

i think you can use js files in unity

#

o

turbid kite