Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webassembly nats client over websocket #530

Closed
frbrno opened this issue Nov 14, 2019 · 9 comments
Closed

webassembly nats client over websocket #530

frbrno opened this issue Nov 14, 2019 · 9 comments
Assignees

Comments

@frbrno
Copy link

frbrno commented Nov 14, 2019

Hi all,
I have written a http server which forwards a raw tcp nats connection to websocket.
The client is written in go, compiled to webassembly, running inside the browser.
I created a nats.CustomDialer and try to connect.
The result is something like this: 'nats: expected PONG, got ?PONG' error origin

So there is an unwanted symbol inside the PONG message. I added a few lines of code in func (nc *Conn) readProto()

func (nc *Conn) readProto() (string, error) {
	var (
		_buf     = [10]byte{}
		buf      = _buf[:0]
		b        = [1]byte{}
		protoEnd = byte('\n')
	)

	for {
		if n, err := nc.conn.Read(b[:1]); err != nil {
			// Do not report EOF error
			if err == io.EOF {
				return string(buf), nil
			}
			return "", err
		} else if n == 0 { //if read count is zero, dont add it to the buf
			continue
		}
		buf = append(buf, b[0])
		if b[0] == protoEnd {
			return string(buf), nil
		}
	}
}

Now i can use the nats client inside the browser and it works like a charm. I am not sure but I think this should be added to the nats client? If read length is zero, do not add it to the buffer.

@kozlovic kozlovic self-assigned this Nov 14, 2019
@kozlovic
Copy link
Member

Thanks, will have a look asap.

@frbrno
Copy link
Author

frbrno commented Nov 14, 2019

nice, thanks. I think there is a reason why conn.Read returns 'n', should not be ignored. 😊

@kozlovic
Copy link
Member

Yes, but returning 0 with err==nil is strange..

@frbrno
Copy link
Author

frbrno commented Nov 14, 2019

yeah, I guess its related to the websocket connection. As I said, I was not sure if its worth to change nats-client code. I ll upload my code soon to github.

@frbrno
Copy link
Author

frbrno commented Nov 15, 2019

Implementations of Read are discouraged from returning a zero byte count with a nil error, except when len(p) == 0. Callers should treat a return of 0 and nil as indicating that nothing happened; in particular it does not indicate EOF. link

it is strange...
discussion
discussion2

my playground

@derekcollison
Copy link
Member

We are close to having our own websocket support right in the server. Will close for now but feel free to re-open.

@tupshin
Copy link

tupshin commented Jun 10, 2020

Even though this is closed, weighing my enthusiasm for implementing this here. I'm building a gui command+control app targeting wasm as one the platforms, and native websocket support would be fantastic.

@derekcollison
Copy link
Member

We have websocket support in master, fyi. Will be in next release but is already in nightly builds.

@oderwat
Copy link
Contributor

oderwat commented Nov 29, 2022

This is related to #1146 because "wasm websocket" is not "js websocket".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants