“Socket io Express” Code-Antworten

Socket.io Node

/*The following example attaches socket.io to a plain Node.JS HTTP server listening on port 3000.*/

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);



//Standalone

const io = require('socket.io')();
io.on('connection', client => { ... });
io.listen(3000);
//Module syntax
import { Server } from "socket.io";
const io = new Server(server);
io.listen(3000);
                               
                               
                               
//In conjunction with Express
//Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function. Also make sure to call .listen on the server, not the app.

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
                               
                               
//In conjunction with Koa
//Like Express.JS, Koa works by exposing an application as a request handler function, but only by calling the callback method.

const app = require('koa')();
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
                               
                               
                               
//In conjunction with Fastify
//To integrate Socket.io in your Fastify application you just need to register fastify-socket.io plugin. It will create a decorator called io.

const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.io.on('connection', () => { /* … */ });
app.listen(3000);
     
Bewildered Badger

Socket IO -Skript

<script src="/socket.io/socket.io.js"></script>
<script>
  const socket = io();
</script>
garzj

So führen Sie Socket.io Server aus

const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const port = process.env.PORT || 8001;
const index = require("./routes/index");
const app = express();
app.use(index);
const server = http.createServer(app);
const io = socketIo(server); // < Interesting!
const getApiAndEmit = "TODO";
Pink Person

JS Socket.Emit

const socket = io('ws://localhost:3000');
socket.on('connect', () => {  
  // either with send()  
  socket.send('Hello!');
  // or with emit() and custom event names  
  socket.emit('salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from([1, 2, 3, 4]));});
// handle the event sent with socket.send()
socket.on('message', data => {
  console.log(data);
});
// handle the event sent with socket.emit()
socket.on('greetings', (elem1, elem2, elem3) => {
  console.log(elem1, elem2, elem3);
});
Hilarious Hoopoe

Socket io nodejs

const express = require('express');const app = express();const http = require('http');const server = http.createServer(app);const { Server } = require("socket.io");const io = new Server(server);app.get('/', (req, res) => {  res.sendFile(__dirname + '/index.html');});io.on('connection', (socket) => {  console.log('a user connected');});server.listen(3000, () => {  console.log('listening on *:3000');});
Quaint Quetzal

Socket io Express

const app = require("express")();const httpServer = require("http").createServer(app);const options = { /* ... */ };const io = require("socket.io")(httpServer, options);io.on("connection", socket => { /* ... */ });httpServer.listen(3000);// WARNING !!! app.listen(3000); will not work here, as it creates a new HTTP server
Jittery Jay

Ähnliche Antworten wie “Socket io Express”

Fragen ähnlich wie “Socket io Express”

Weitere verwandte Antworten zu “Socket io Express” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen