shihuojian commited on
Commit
ee4ce11
·
1 Parent(s): 96020f0

配置socket

Browse files
hapi-think/index.js CHANGED
@@ -585,12 +585,13 @@ module.exports = async (app) =>{
585
  },{
586
  plugin: require('./cron'),
587
  options: {}
588
- },{
589
- plugin:require('./socket'),
590
- options:{}
591
  }
592
  ]);
593
 
 
 
 
 
594
  return server;
595
  }
596
  };
 
585
  },{
586
  plugin: require('./cron'),
587
  options: {}
 
 
 
588
  }
589
  ]);
590
 
591
+ await server.register({
592
+ plugin:require('./socket'),
593
+ options:{}
594
+ })
595
  return server;
596
  }
597
  };
hapi-think/lib/basic.js CHANGED
@@ -10,7 +10,9 @@ module.exports = class{
10
  const xFF = headers['x-forwarded-for'];
11
  const remoteAddress = xFF ? xFF.split(',')[0] : info.remoteAddress;
12
  const user = request.auth.credentials;
13
- const cookies = new Cookies(req, res)
 
 
14
  this.ctx = {
15
  db:(collection,options={})=>mongodb(request,collection,options),
16
  user:!_.isEmpty(user) ? {_id:user._id,name:user.username,source:user.source} : user,
@@ -35,7 +37,7 @@ module.exports = class{
35
  isGet:request.method === 'get' ? true : null,
36
  isPost:request.method === 'post' ? true : null,
37
  upload:uploadClient,
38
- socket:request.socket || {},
39
  cookie:{ //see https://github.com/pillarjs/cookies#readme
40
  set:(key,value,options)=>{
41
  return cookies.set(key,value,options)
 
10
  const xFF = headers['x-forwarded-for'];
11
  const remoteAddress = xFF ? xFF.split(',')[0] : info.remoteAddress;
12
  const user = request.auth.credentials;
13
+ const cookies = new Cookies(req, res);
14
+ const socketSetting = request.route.settings.plugins['socket'];
15
+ const socket = socketSetting && socketSetting.enabled && socketSetting.io ? socketSetting.io : {}
16
  this.ctx = {
17
  db:(collection,options={})=>mongodb(request,collection,options),
18
  user:!_.isEmpty(user) ? {_id:user._id,name:user.username,source:user.source} : user,
 
37
  isGet:request.method === 'get' ? true : null,
38
  isPost:request.method === 'post' ? true : null,
39
  upload:uploadClient,
40
+ socket,
41
  cookie:{ //see https://github.com/pillarjs/cookies#readme
42
  set:(key,value,options)=>{
43
  return cookies.set(key,value,options)
hapi-think/route.js CHANGED
@@ -30,7 +30,7 @@ exports.plugin = {
30
  if(!route){
31
  const options = collection === "api" ? {auth: 'auth'} : {}
32
  server.route({
33
- method: collection === "api" ? ['POST','GET'] : "GET",
34
  path: collection === "api" ? `/api/${_id}` : `/${_id}`,
35
  options,
36
  handler: async (request,h) => {
@@ -69,7 +69,7 @@ exports.plugin = {
69
  let route = item.route ? JSON.parse(item.route) : {};
70
  const {method,path,options} = route;
71
  server.route({
72
- method:method || ['POST','GET'],
73
  path: `/api/${_id}`, //不要支持修改path,如果多个path一样会出问题,需要的话只能单独写个短连接配置~
74
  options:{auth: 'auth',...options},
75
  handler: async (request,h) => {
 
30
  if(!route){
31
  const options = collection === "api" ? {auth: 'auth'} : {}
32
  server.route({
33
+ method: collection === "api" ? 'GET' : "GET",
34
  path: collection === "api" ? `/api/${_id}` : `/${_id}`,
35
  options,
36
  handler: async (request,h) => {
 
69
  let route = item.route ? JSON.parse(item.route) : {};
70
  const {method,path,options} = route;
71
  server.route({
72
+ method:method || 'GET',
73
  path: `/api/${_id}`, //不要支持修改path,如果多个path一样会出问题,需要的话只能单独写个短连接配置~
74
  options:{auth: 'auth',...options},
75
  handler: async (request,h) => {
hapi-think/socket.js CHANGED
@@ -12,14 +12,21 @@ exports.plugin = {
12
  // await server.register(require('@hapi/nes'));
13
 
14
  //see https://socket.io/docs/v4/server-options/
15
- const io = new Server(server.listener,{});
16
- io.on("connection", (socket) => {
17
- // console.log('hello socket')
 
 
 
 
 
 
 
 
 
 
 
18
  });
19
- server.decorate('request', 'socket', io);
20
-
21
  }
22
-
23
-
24
 
25
  };
 
12
  // await server.register(require('@hapi/nes'));
13
 
14
  //see https://socket.io/docs/v4/server-options/
15
+ const table = server.table();
16
+ table.forEach(item => {
17
+ const settings = item.settings.plugins['socket'];
18
+ if(settings && settings.enabled){
19
+ const {path} = item;
20
+ const io = new Server(server.listener,{
21
+ ...settings,
22
+ path,
23
+ });
24
+ io.on("connection", (socket) => {
25
+ // console.log('hello socket')
26
+ });
27
+ item.settings.plugins['socket'].io = io
28
+ }
29
  });
 
 
30
  }
 
 
31
 
32
  };
src/pages/api/ZhIWjVHdVLru7eFn.js CHANGED
@@ -1,8 +1,9 @@
1
 
2
 
3
  export default async (req)=>{
4
- const {send,reply,error,isGet,db,user,mongo,cookie,event} = await req.ctx();
5
  try{
 
6
  send("success")
7
  }catch (e){
8
  error(e)
 
1
 
2
 
3
  export default async (req)=>{
4
+ const {send,reply,error,isGet,db,user,mongo,cookie,event,socket} = await req.ctx();
5
  try{
6
+ socket.emit('tttt','66666')
7
  send("success")
8
  }catch (e){
9
  error(e)
src/pages/web/ZhkJRJZe81m1AB-0/index.js CHANGED
@@ -5,10 +5,11 @@ const io = require('socket.io-client');
5
  export default function App() {
6
  const start = async () => {
7
 
8
- const ioClient = io.connect('http://localhost:3001/', {
 
9
  transports: ['websocket']
10
  });
11
- ioClient.on("tttt", (value) => {
12
  console.log(value)
13
  })
14
  };
 
5
  export default function App() {
6
  const start = async () => {
7
 
8
+ const socket = io.connect('http://localhost:3001', {
9
+ path:"/api/ZhIWjVHdVLru7eFn",
10
  transports: ['websocket']
11
  });
12
+ socket.on("tttt", (value) => {
13
  console.log(value)
14
  })
15
  };
src/pages/web/ZhovF-z7MBtcx2UQ/index.js ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Container from './style';
2
+ import {useState,useEffect} from 'react';
3
+ const io = require('socket.io-client');
4
+
5
+ export default function App() {
6
+ const start = async () => {
7
+
8
+ const socket = io.connect('http://localhost:3001', {
9
+ path:"/api/ZhNE2aqY2Pr2uvqA",
10
+ transports: ['websocket']
11
+ });
12
+ socket.on("tttt", (value) => {
13
+ console.log(value)
14
+ })
15
+ };
16
+
17
+ useEffect(() => {
18
+ start();
19
+ },[]);
20
+
21
+ return (
22
+ <Container>
23
+ test111
24
+ </Container>
25
+ )
26
+ }
src/pages/web/ZhovF-z7MBtcx2UQ/style.js ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import { styled } from '@mui/material/styles';
2
+ export default styled('div')({
3
+
4
+ });
src/pages/web/ZhovF-z7MBtcx2UQ/style.module.scss ADDED
@@ -0,0 +1 @@
 
 
1
+