Created
July 12, 2020 01:59
-
-
Save misterpah/b3972f809437676e6123f6dac1ccb48c to your computer and use it in GitHub Desktop.
golang websocket client connect with nodejs websocket server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
"github.com/gorilla/websocket" | |
) | |
func main() { | |
connectToNodejsWebsocketServer() | |
fmt.Println("closing app") | |
} | |
// Client used for websocket | |
type Client struct { | |
// The websocket connection. | |
conn *websocket.Conn | |
// Buffered channel of outbound messages. | |
send chan []byte | |
} | |
var ( | |
newline = []byte{'\n'} | |
space = []byte{' '} | |
) | |
const ( | |
// Time allowed to write a message to the peer. | |
writeWait = 10 * time.Second | |
// Time allowed to read the next pong message from the peer. | |
pongWait = 60 * time.Second | |
// Send pings to peer with this period. Must be less than pongWait. | |
pingPeriod = (pongWait * 9) / 10 | |
// Maximum message size allowed from peer. | |
maxMessageSize = 65536 | |
) | |
func (c *Client) readPump() { | |
defer func() { | |
c.conn.Close() | |
}() | |
c.conn.SetReadLimit(maxMessageSize) | |
c.conn.SetReadDeadline(time.Now().Add(pongWait)) | |
c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) | |
for { | |
_, message, err := c.conn.ReadMessage() | |
if err != nil { | |
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { | |
log.Printf("error: %v", err) | |
} | |
break | |
} | |
fmt.Println(string(message)) | |
//message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1)) | |
} | |
} | |
func connectToNodejsWebsocketServer() { | |
url := "your.ip.addess.here:port/foo" | |
ws, res, err := websocket.DefaultDialer.Dial("ws://"+url, nil) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(res) | |
client := &Client{conn: ws, send: make(chan []byte, 256)} | |
go client.readPump() | |
for { | |
// infinite loop so our client can wait for read/write from/to server | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const WebSocket = require('ws'); | |
const url = require('url'); | |
const server = http.createServer(); | |
const wss1 = new WebSocket.Server({ noServer: true }); | |
const wss2 = new WebSocket.Server({ noServer: true }); | |
wss1.on('connection', function connection(ws) { | |
// ... | |
ws.send("hello websocket /foo") | |
}); | |
wss2.on('connection', function connection(ws) { | |
// ... | |
ws.send("hello websocket /bar") | |
}); | |
server.on('upgrade', function upgrade(request, socket, head) { | |
const pathname = url.parse(request.url).pathname; | |
if (pathname === '/foo') { | |
wss1.handleUpgrade(request, socket, head, function done(ws) { | |
wss1.emit('connection', ws, request); | |
}); | |
} else if (pathname === '/bar') { | |
wss2.handleUpgrade(request, socket, head, function done(ws) { | |
wss2.emit('connection', ws, request); | |
}); | |
} else { | |
socket.destroy(); | |
} | |
}); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment