我在此试图开展以下工作:
- express: 4.14
- socket.io: 1.5
- passport (using sessions): 0.3
- redis: 2.6 (Really fast data structure to handle sessions; but you can use others like MongoDB too. However, I encourage you to use this for session data + MongoDB to store other persistent data like Users)
由于你可能希望增加一些APIC申请,我们也使用<>strong>http://的包裹,使吉大港山区和网络的袖珍在同一港口工作。
server.js
下面的摘录仅包括你需要建立以前技术的一切。 你可以看到完整的服务器。 jjs edition which I used in one of my projects 。
import http from http ;
import express from express ;
import passport from passport ;
import { createClient as createRedisClient } from redis ;
import connectRedis from connect-redis ;
import Socketio from socket.io ;
// Your own socket handler file, it s optional. Explained below.
import socketConnectionHandler from ./sockets ;
// Configuration about your Redis session data structure.
const redisClient = createRedisClient();
const RedisStore = connectRedis(Session);
const dbSession = new RedisStore({
client: redisClient,
host: localhost ,
port: 27017,
prefix: stackoverflow_ ,
disableTTL: true
});
// Let s configure Express to use our Redis storage to handle
// sessions as well. You ll probably want Express to handle your
// sessions as well and share the same storage as your socket.io
// does (i.e. for handling AJAX logins).
const session = Session({
resave: true,
saveUninitialized: true,
key: SID , // this will be used for the session cookie identifier
secret: secret key ,
store: dbSession
});
app.use(session);
// Let s initialize passport by using their middlewares, which do
//everything pretty much automatically. (you have to configure login
// / register strategies on your own though (see reference 1)
app.use(passport.initialize());
app.use(passport.session());
// Socket.IO
const io = Socketio(server);
io.use((socket, next) => {
session(socket.handshake, {}, next);
});
io.on( connection , socketConnectionHandler);
// socket.io is ready; remember that ^this^ variable is just the
// name that we gave to our own socket.io handler file (explained
// just after this).
// Start server. This will start both socket.io and our optional
// AJAX API in the given port.
const port = 3000; // Move this onto an environment variable,
// it ll look more professional.
server.listen(port);
console.info(`? API listening on port ${port}`);
console.info(`? Socket listening on port ${port}`);
sockets/index.js
我们的<代码>socketConnectionHandler,我不喜欢把一切放在服务器内(尽管你完全可以做到这一点),特别是因为这一档案能够很快地包含大量的代码。
export default function connectionHandler(socket) {
const userId = socket.handshake.session.passport &&
socket.handshake.session.passport.user;
// If the user is not logged in, you might find ^this^
// socket.handshake.session.passport variable undefined.
// Give the user a warm welcome.
console.info(`⚡︎ New connection: ${userId}`);
socket.emit( Grettings , `Grettings ${userId}`);
// Handle disconnection.
socket.on( disconnect , () => {
if (process.env.NODE_ENV !== production ) {
console.info(`⚡︎ Disconnection: ${userId}`);
}
});
}
Extra material (client):
简言之,简言之,简言之。 用户可以是:
import io from socket.io-client ;
const socketPath = /socket.io ; // <- Default path.
// But you could configure your server
// to something like /api/socket.io
const socket = io.connect( localhost:3000 , { path: socketPath });
socket.on( connect , () => {
console.info( Connected );
socket.on( Grettings , (data) => {
console.info(`Server gretting: ${data}`);
});
});
socket.on( connect_error , (error) => {
console.error(`Connection error: ${error}`);
});
References:
I 只是略微提及该守则,因此我在此提出。
1. 如何制定你的护照战略: https://scotch.io/tutorials/easy-node-authentication-setup-and- local#handling-signup Registration