Skip to content

Instantly share code, notes, and snippets.

@DezChuang
Last active November 11, 2019 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DezChuang/ad028099fafda4267a8ff2f9defbbc7f to your computer and use it in GitHub Desktop.
Save DezChuang/ad028099fafda4267a8ff2f9defbbc7f to your computer and use it in GitHub Desktop.
// 建立一個 WebSocket 物件,並連上 socket server
const ws = new WebSocket('ws://localhost:5566')
// 連線建立後
ws.onopen = () => {
console.log('open connection to server')
}
// 連線斷開後
ws.onclose = () => {
console.log('close connection to server')
}
// 收到 server 事件時,將事件中的訊息印出來
ws.onmessage = event => {
console.log(event.data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment