Commit c8fb0d60 authored by huanglu's avatar huanglu

feat: update

parent ab1f5e38
...@@ -5,35 +5,29 @@ export default class NspController extends Controller { ...@@ -5,35 +5,29 @@ export default class NspController extends Controller {
const { ctx, app } = this; const { ctx, app } = this;
const nsp = app.io.of('/') as any; const nsp = app.io.of('/') as any;
const message = ctx.args[0] || {}; const message = ctx.args[0] || {};
const socket = ctx.socket; const socketId = ctx.socket.id;
const client = socket.id; const fn = ctx.args[1];
try { try {
const { target, payload, groupId } = message; const { target, groupId } = message;
const group = await ctx.helper.queryGroup(app.redis, groupId); const group = await ctx.helper.queryGroup(app.redis, groupId);
if (nsp.sockets) {
Object.keys(nsp.sockets).forEach((id: string) => {
nsp.sockets[id] && socketId !== id && nsp.sockets[id].emit('msg', message);
});
fn && fn({
code: 0, message: '消息已收到', tm: new Date().getTime(),
});
return;
}
if (group) { // 聊天室 if (group) { // 聊天室
group.groups.forEach((sck: string) => { (group.groups || []).forEach((sck: string) => {
nsp.sockets[sck] && nsp.sockets[sck].emit('msg', message); nsp.sockets[sck] && nsp.sockets[sck].emit('msg', message);
}); });
} else { // 单聊 } else { // 单聊
const msg = ctx.helper.parseMsg('exchange', payload, { client, target }); target && nsp.sockets[target].emit('msg', message);
target && nsp.sockets[target].emit('msg', msg);
} }
} catch (error) { } catch (error) {
app.logger.error(error); app.logger.error(error);
} }
} }
async chatList() {
const { ctx, app } = this;
const nsp = app.io.of('/') as any;
const message = ctx.args[0] || {};
const socket = ctx.socket;
try {
const { target } = message;
if (!target) return;
socket.emit('msg', nsp.sockets);
} catch (error) {
app.logger.error(error);
}
}
} }
\ No newline at end of file
...@@ -2,7 +2,14 @@ import { Context } from 'egg'; ...@@ -2,7 +2,14 @@ import { Context } from 'egg';
export default () => { export default () => {
return async (_ctx: Context, next: any) => { return async (_ctx: Context, next: any) => {
console.log('链接成功', _ctx.socket.id);
// _ctx.socket.on('msg', (value: any, fn: any) => {
// console.log('链接成功: ', value);
// // console.log('链接成功: fn ===', fn);
// fn('woot');
// });
await next(); await next();
// 断开连接后,删除对应的 群聊信息 // 断开连接后,删除对应的 群聊信息
console.log('链接成功失败', _ctx.socket.id);
}; };
}; };
import { Context } from 'egg'; import { Context } from 'egg';
export default () => { export default () => {
return async (ctx: Context, next: any) => { return async (_ctx: Context, next: any) => {
ctx.socket.emit('res', {
time: new Date().getTime(),
success: 'received',
});
await next(); await next();
}; };
}; };
...@@ -2,6 +2,5 @@ import { Application } from 'egg'; ...@@ -2,6 +2,5 @@ import { Application } from 'egg';
export default (app: Application) => { export default (app: Application) => {
const { io } = app; const { io } = app;
io.of('/').route('msg', (io.controller as any).nsp.send); io.of('/').route('msg', (io.controller as any).nsp.send);
}; };
import { EggAppConfig, EggAppInfo, PowerPartial, Request } from 'egg'; import { EggAppConfig, EggAppInfo, PowerPartial, Request } from 'egg';
import * as qs from 'qs'; import * as qs from 'qs';
const redisConfig = { // const redisConfig = {
host: '192.168.32.34', // host: '192.168.32.34',
port: 6379, // port: 6379,
password: '123456', // password: '123456',
}; // };
export default (appInfo: EggAppInfo) => { export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>; const config = {} as PowerPartial<EggAppConfig>;
...@@ -32,18 +32,18 @@ export default (appInfo: EggAppInfo) => { ...@@ -32,18 +32,18 @@ export default (appInfo: EggAppInfo) => {
}, },
generateId: (req: Request) => { // 自定义 socket.id 生成函数 generateId: (req: Request) => { // 自定义 socket.id 生成函数
const data = qs.parse(req.url.split('?')[1]); const data = qs.parse(req.url.split('?')[1]);
return data.userId; return data.userId || new Date().getTime().toFixed(0);
},
redis: {
...redisConfig,
},
};
const redis = {
client: {
...redisConfig,
db: 0,
}, },
// redis: {
// ...redisConfig,
// },
}; };
// const redis = {
// client: {
// ...redisConfig,
// db: 0,
// },
// };
const cors = { const cors = {
origin: '*', origin: '*',
credentials: true, credentials: true,
...@@ -54,7 +54,7 @@ export default (appInfo: EggAppInfo) => { ...@@ -54,7 +54,7 @@ export default (appInfo: EggAppInfo) => {
...bizConfig, ...bizConfig,
view, view,
io, io,
redis, // redis,
cors, cors,
security: { security: {
csrf: { csrf: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment