Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created June 10, 2018 03:43
Show Gist options
  • Save tabvn/d79d3b6820bf39367afc3e3be53dfd7a to your computer and use it in GitHub Desktop.
Save tabvn/d79d3b6820bf39367afc3e3be53dfd7a to your computer and use it in GitHub Desktop.
uWs websocket as Client for server (Node.js)
import WebSocket from 'uws'
import { config } from './config'
import _ from 'lodash'
export default class WebSocketClient {
constructor (options) {
this.connect = this.connect.bind(this)
this.reconnect = this.reconnect.bind(this)
this.sendMessageQueue = this.sendMessageQueue.bind(this)
this.queue = [] // keep query sending
this.ws = null
this.url = _.get(options, 'url')
this.isConnected = false
if (_.get(options, 'autoConnect')) {
this.connect()
}
}
connect () {
if (this.isConnected) {
return
}
this.ws = new WebSocket(this.url ? this.url : config.socketServer)
this.ws.onopen = () => {
console.log('Connected to server.')
this.isConnected = true
// after connected we may need check queue and send
this.sendMessageQueue()
}
this.ws.onclose = () => {
console.log('Disconnected')
this.isConnected = false
this.reconnect()
}
this.ws.onerror = () => {
this.isConnected = false
this.reconnect()
console.log('Connection error')
}
}
reconnect () {
console.log('Begin reconnecting')
this.connect()
}
send (message) {
if (!this.isConnected) {
// keep message to queue
this.queue.push(message)
}
try {
const messageStr = JSON.stringify(message)
this.ws.send(messageStr)
} catch (e) {
console.log('An error convert object to string')
}
}
sendMessageQueue () {
if (this.queue.length === 0) {
return
}
for (let i = 0; i < this.queue.length; i++) {
const message = this.queue[i]
if (message) {
this.send(this.queue[i])
delete this.queue[i]
}
}
}
}
@tabvn
Copy link
Author

tabvn commented Jun 10, 2018

How to use

const ws = new WebSocketClient({url: 'ws://localhost:3000', autoConnect: true})

const message = {action: 'something', payload: 'something...'}

ws.send(message) // send a message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment