Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
egg-websocket
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huanglu
egg-websocket
Commits
c8fb0d60
Commit
c8fb0d60
authored
Dec 16, 2020
by
huanglu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: update
parent
ab1f5e38
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
43 deletions
+38
-43
nsp.ts
app/io/controller/nsp.ts
+14
-21
auth.ts
app/io/middleware/auth.ts
+7
-0
packet.ts
app/io/middleware/packet.ts
+1
-5
socket-router.ts
app/socket-router.ts
+0
-1
config.default.ts
config/config.default.ts
+16
-16
No files found.
app/io/controller/nsp.ts
View file @
c8fb0d60
...
@@ -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
socket
Id
=
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
app/io/middleware/auth.ts
View file @
c8fb0d60
...
@@ -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
);
};
};
};
};
app/io/middleware/packet.ts
View file @
c8fb0d60
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
();
};
};
};
};
app/socket-router.ts
View file @
c8fb0d60
...
@@ -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
);
};
};
config/config.default.ts
View file @
c8fb0d60
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
:
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment