Giter Club home page Giter Club logo

socketiomanual's Introduction

SocketIO Manual

คู่มือการใช้งาน Socket.io ฉบับ YoProgrammer

Table of Contents

  • Create Server
  • Create Client
  • Server Event
    • io.on('connection')(socket => {})
      • socket.id
      • socket.emit()
      • socket.broadcast.emit()
      • socket.on() # send callback ext. socket.on('event', (msg, cb) => {cb()})
        • io.emit()
        • socket.join()
          • socket.emit()
          • io.to().emit()
          • socket.broadcast.to().emit()
        • socket.leave()
  • Client Event
    • socket.on('connect')
    • socket.on('disconnect)
    • socket.on()
    • socket.emit() # recive callback ext. socket.emit({}, function(){})

Create Server

const express = require('express')
const http = require('http')
const path = require('path')
const socketIO = require('socket.io')

const app = express()

const server = http.createServer(app);
const io = socketIO(server)

io.on('connection', socket => {
  console.log('New user connect')

  socket.on('disconnect', () => {
    console.log('User was disconnected')
  })
})

app.use(express.static(path.join(__dirname, '..', 'public')))

const PORT = process.env.PORT || 3000
server.listen(PORT)
console.log(`Server is up on ${PORT}`)

Create Client

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Socket.io</title>
</head>
<body>
  Hello
  <script src="/socket.io/socket.io.js"></script>
  <script>
    const socket = io()
    socket.on('connect', () => {
      console.log('Connect to server')
    })

    socket.on('disconnect', () => {
      console.log('Disconnect from server')
    })
  </script>
</body>
</html>

socketiomanual's People

Contributors

yuttasakcom avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.