Sunday, April 28, 2024
HomeGolangGorilla Internet Sockets reside doesn't work - Getting Assist

Gorilla Internet Sockets reside doesn’t work – Getting Assist


I reduce and pasted a pattern code from right here This code works out of the field on my native laptop.

However when shifting to a VPS with Nginx proxy this fails. As this “converts” from https:// to wss:// I’ve no clue what I’ve accomplished unsuitable. https://ws.go4webdev.org (reside)

image

The console offers this error:

WebSocket connection to ‘wss://localhost:8080/echo’ failed:

And the web page exhibits up like this:

image

I believe that it’s this line that’s the downside, however I can’t work out the syntax.

//var addr = flag.String("addr", "127.0.0.1:8080", "http service handle") <-- works on my native laptop

var addr = flag.String("addr", "https://ws.go4webdev.org", "http service handle") <-- fails on VPS

My intestine feeling tells me that that is near a CORS error, however I can’t verify.

Any clue how I could make this work?

The reply lies in your template:

homeTemplate.Execute(w, "ws://"+r.Host+"/echo")

Are you working this behind a reverse proxy or one thing? A easy repair could be to do away with that a part of the template and as a substitute push the logic to the browser. One thing like this:

ws = new WebSocket("ws://" + doc.location.host + "/echo");

Thanks, however when transfer this to the javascript there isn’t any worth assigned to the ws. I’ve up to date the javascript and added some alerts and right here it’s reside (nonetheless not working).

It appears to me that the web page is loaded twice and perhaps clear the worth of ws?

window.addEventListener("load", perform(evt) {
  alert("worth of ws at load " + ws)
  var output = doc.getElementById("output");
  var enter = doc.getElementById("enter");
  var ws;
  var print = perform(message) {
    var d = doc.createElement("div");
    d.textContent = message;
    output.appendChild(d);
    output.scroll(0, output.scrollHeight);
  };

  doc.getElementById("open").onclick = perform(evt) {
    alert("worth at ws at open: " + ws)

    if (typeof ws !== 'undefined' && ws !== null) {
      return
    }
    
    alert("assign worth to ws")
    ws = new WebSocket("ws://" + doc.location.host + "/echo");
    alert("ws://" + doc.location.host + "/echo") // this isn't executed for some cause...
    alert(ws);

    ws.onopen = perform(evt) {
      print("OPEN");
    }
    ws.onclose = perform(evt) {
      print("CLOSE");
      ws = null;
    }
    ws.onmessage = perform(evt) {
      print("RESPONSE: " + evt.knowledge);
    }
    ws.onerror = perform(evt) {
      print("ERROR: " + evt.knowledge);
    }
    return false;
  };
  doc.getElementById("ship").onclick = perform(evt) {
    if (!ws) {
      return false;
    }
    print("SEND: " + enter.worth);
    ws.ship(enter.worth);
    return false;
  };
  doc.getElementById("shut").onclick = perform(evt) {
    if (!ws) {
      return false;
    }
    ws.shut();
    return false;
  };
});

Possibly you want wss. I had a WebSocket venture as soon as and when deployed behind a reverse proxy (nginx), it was accessed by way of wss. Listed below are a few code snippets:

// The websocket URL scheme. 
var wsScheme="{{.WSScheme}}";
...
  wsConn =  new WebSocket(wsScheme+"//" + wsUrl +"/socket");
	w.Header().Set("Content material-Sort", "textual content/html")

	t,err := template.ParseFiles("take a look at.html")
	if err != nil {
		http.Error(w, fmt.Sprintf("parse file error: %s", err), http.StatusInternalServerError)
		return
	}

	var scheme struct {
		WSScheme string
	}
	if (!s.RunningLocal) {
		scheme.WSScheme="wss:"
	} else {
		scheme.WSScheme="ws:"
	}

	err = t.Execute(w, scheme)
	if err != nil {
		http.Error(w, fmt.Sprintf("template error: %s", err), http.StatusInternalServerError)
		return
	}

I’m questioning if, since you will have these buttons inside a type, they’re trying to submit the shape once you press them. Strive altering these buttons to button sort="button" in order that they don’t submit the shape, inflicting a reload.

Thanks! I did this and received ONE step additional :slight_smile:

Now a minimum of the console exhibits one error:

ws.js:21 WebSocket connection to ‘wss://ws.go4webdev.org/echo’ failed: Error throughout WebSocket handshake: Sudden response code: 400

Any clue how I observe down this?

I believe I discovered one resolution. It appears that evidently Websockets is an outdated protocol that’s solely supported by HTTP 1.1 (can’t verify this). However when added this to the Nginx sites-available, it appears to work.

    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Improve $http_upgrade;
        proxy_set_header Connection "improve";
        proxy_set_header Host $host;
    }

If anyone can verify this, I needs to be good to know.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments