Golang : Serving HTTP and Websocket from different ports in a program example
Problem:
For some reason you want to split the port use by your program to serve HTTP and Web-socket traffic. How to write one program that is able to listen and serve HTTP/Web-socket traffics on different ports?
Solution:
Setup a http.NewServeMux()
and http.ListenAndServe()
for one port and launch it as go-routine. For example, one go-routine for handling web socket traffic and main routine in the program to handle HTTP traffic.
NOTE : Instead of web-socket, can you use for another HTTP server? Yes, of course.
Here you go!
package main
import (
"fmt"
"golang.org/x/net/websocket"
"io"
"net/http"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
html := `<!DOCTYPE html>
<html>
<head>
<title>One HTTP and Websocket serve with different ports example</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(function() {
var ws = new WebSocket("ws://localhost:8082/"); // use port 8082 instead of 8080
var $ul = $('#msg-list');
$('#sendBtn').click(function(){
var data = $('#name').val();
ws.send(data);
console.log("Sending data to HTTP server via Websocket via port 8082 :" + data);
$('<li>').text(data).appendTo($ul);
});
});
</script>
</head>
<body>
<input id="name" type="text" />
<input type="button" id="sendBtn" value="send"></input>
<ul id="msg-list"></ul>
</body></html>`
w.Write([]byte(fmt.Sprintf(html)))
}
func wsHandler(ws *websocket.Conn) {
fmt.Println("Receive data from : ", ws.LocalAddr())
fmt.Println("Sending data to : ", ws.RemoteAddr())
// echo back
io.Copy(ws, ws)
}
func main() {
mxWS := http.NewServeMux()
mxWS.Handle("/", websocket.Handler(wsHandler))
fmt.Println("Listen and serve websocket on :8082")
go func() {
http.ListenAndServe(":8082", mxWS)
}()
mxHTTP := http.NewServeMux()
mxHTTP.HandleFunc("/", homeHandler)
fmt.Println("Listen and serve HTTP on :8080")
http.ListenAndServe(":8080", mxHTTP)
}
Run the code and point your browser to http://localhost:8080
. Open up the developer view to see the console messages.
References:
https://www.socketloop.com/tutorials/golang-web-javascript-to-server-side-websocket-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.4k Golang : flag provided but not defined error
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+32.4k Golang : Call a function after some delay(time.Sleep and Tick)
+26.8k Golang : Connect to database (MySQL/MariaDB) server
+10.3k Golang : How to flush a channel before the end of program?
+14.2k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+5.2k Golang : Detect face in uploaded photo like GPlus
+11.9k Golang : Handle or parse date string with Z suffix(RFC3339) example
+6.1k Nginx : How to block user agent ?
+4k Golang : Print instead of building pyramids
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+12.9k Golang : Google Drive API upload and rename example