Socket.IO技術應用於Vue.js + Node.js專案(1)

前言

什麼是Socket.IO?

//前端
socket.emit("greetingFromClient","Hello, Backend!")
//後端
socket.on("greetingFromClient",function (msg) {
console.log(msg) //Hello, Backend!
socket.emit("greetingFromServer","Hello,Frontend!")
})
//前端
socket.on("greetingFromServer",function (msg) {
console.log(msg) //Hello,Frontend!
})
socket.emit('msg', "Hello"); //sending to sender-client onlysocket.broadcast.emit('msg', "Hello"); //sending to all clients except sendersocket.broadcast.to('game').emit('message', 'Hello'); //sending to all clients in 'game' room(channel) except sendersocket.to('game').emit('msg', 'Hello'); //sending to sender client, only if they are in 'game' room(channel)socket.broadcast.to(socketid).emit('msg', 'Hello'); //sending to individual socketidio.emit('msg', "Hello"); //sending to all clients, include senderio.in('game').emit('msg', 'Hello'); //sending to all clients in 'game' room(channel), include sendersocket.on(); //event listener, can be called on client to execute on server

--

--

1992, 學習科技|研究投資|環境工程技師|成功大學環境工程學系|台灣大學環境工程研究所|水處理行業工作兩年半|想要用科技打造自己的企業,過自己想過的生活。

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Harry Lu

1992, 學習科技|研究投資|環境工程技師|成功大學環境工程學系|台灣大學環境工程研究所|水處理行業工作兩年半|想要用科技打造自己的企業,過自己想過的生活。