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
ab1f5e38
Commit
ab1f5e38
authored
Dec 15, 2020
by
huanglu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 优化聊天室
parent
b6e74853
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
148 additions
and
25 deletions
+148
-25
news.ts
app/controller/news.ts
+0
-9
socket.ts
app/controller/socket.ts
+18
-0
helper.ts
app/extend/helper.ts
+39
-1
nsp.ts
app/io/controller/nsp.ts
+12
-5
auth.ts
app/io/middleware/auth.ts
+1
-5
packet.ts
app/io/middleware/packet.ts
+4
-2
router.ts
app/router.ts
+4
-0
socket.ts
app/service/socket.ts
+65
-0
socket-router.ts
app/socket-router.ts
+1
-1
index.d.ts
typings/app/controller/index.d.ts
+2
-2
index.d.ts
typings/app/service/index.d.ts
+2
-0
No files found.
app/controller/news.ts
deleted
100644 → 0
View file @
b6e74853
import
{
Controller
}
from
'egg'
;
export
default
class
HomeController
extends
Controller
{
public
async
list
()
{
const
{
ctx
}
=
this
;
const
data
=
await
ctx
.
service
.
news
.
list
();
await
this
.
ctx
.
render
(
'list.nj'
,
data
);
}
}
app/controller/socket.ts
0 → 100644
View file @
ab1f5e38
import
{
Controller
}
from
'egg'
;
export
default
class
HomeController
extends
Controller
{
public
async
createRoom
()
{
const
{
ctx
}
=
this
;
ctx
.
body
=
await
ctx
.
service
.
socket
.
createRoom
();
}
public
async
joinRoom
()
{
const
{
ctx
}
=
this
;
ctx
.
body
=
await
ctx
.
service
.
socket
.
joinRoom
();
}
public
async
queryList
()
{
const
{
ctx
}
=
this
;
ctx
.
body
=
await
ctx
.
service
.
socket
.
queryRooms
();
}
}
app/extend/helper.ts
View file @
ab1f5e38
...
@@ -3,7 +3,7 @@ export default {
...
@@ -3,7 +3,7 @@ export default {
const
meta
=
Object
.
assign
({},
{
const
meta
=
Object
.
assign
({},
{
timestamp
:
Date
.
now
(),
timestamp
:
Date
.
now
(),
},
metadata
);
},
metadata
);
console
.
log
((
this
as
any
).
redis
);
return
{
return
{
meta
,
meta
,
data
:
{
data
:
{
...
@@ -12,4 +12,42 @@ export default {
...
@@ -12,4 +12,42 @@ export default {
},
},
};
};
},
},
async
cacheGroupId
(
redis
:
any
,
groupId
:
string
,
socketId
:
string
,
groupName
:
string
)
{
const
data
=
await
redis
.
get
(
'gk_chat_room'
);
if
(
data
)
{
const
list
=
JSON
.
parse
(
data
)
||
[];
const
targetGroup
=
list
.
find
((
itm
:
any
)
=>
itm
.
groupId
===
groupId
);
if
(
targetGroup
)
{
const
sockets
=
targetGroup
.
groups
||
[];
sockets
.
push
(
socketId
);
await
redis
.
set
(
'gk_chat_room'
,
JSON
.
stringify
(
list
));
return
;
}
}
const
value
=
JSON
.
stringify
([{
groupId
,
groupName
,
groups
:
[
socketId
,
],
}]);
await
redis
.
set
(
'gk_chat_room'
,
value
);
},
async
queryGroup
(
redis
:
any
,
groupId
:
string
)
{
const
data
=
await
redis
.
get
(
'gk_chat_room'
);
const
list
=
JSON
.
parse
(
data
||
'[]'
)
||
[];
const
group
=
list
.
find
((
itm
:
any
)
=>
itm
.
groupId
===
groupId
)
||
{};
return
group
;
},
async
removeIdFromGroup
(
redis
:
any
,
groupId
:
string
,
socketId
:
string
)
{
const
data
=
await
redis
.
get
(
'gk_chat_room'
);
if
(
data
)
{
const
list
=
JSON
.
parse
(
data
)
||
[];
const
targetGroup
=
list
.
find
((
itm
:
any
)
=>
itm
.
groupId
===
groupId
);
if
(
targetGroup
)
{
const
sockets
=
(
targetGroup
.
groups
||
[]).
filter
((
itm
:
any
)
=>
itm
!==
socketId
);
targetGroup
.
groups
=
sockets
;
await
redis
.
set
(
'gk_chat_room'
,
JSON
.
stringify
(
list
));
}
}
},
};
};
app/io/controller/nsp.ts
View file @
ab1f5e38
...
@@ -8,10 +8,16 @@ export default class NspController extends Controller {
...
@@ -8,10 +8,16 @@ export default class NspController extends Controller {
const
socket
=
ctx
.
socket
;
const
socket
=
ctx
.
socket
;
const
client
=
socket
.
id
;
const
client
=
socket
.
id
;
try
{
try
{
const
{
target
,
payload
}
=
message
;
const
{
target
,
payload
,
groupId
}
=
message
;
if
(
!
target
)
return
;
const
group
=
await
ctx
.
helper
.
queryGroup
(
app
.
redis
,
groupId
);
const
msg
=
ctx
.
helper
.
parseMsg
(
'exchange'
,
payload
,
{
client
,
target
});
if
(
group
)
{
// 聊天室
nsp
.
sockets
[
target
].
emit
(
'msg'
,
msg
);
group
.
groups
.
forEach
((
sck
:
string
)
=>
{
nsp
.
sockets
[
sck
]
&&
nsp
.
sockets
[
sck
].
emit
(
'msg'
,
message
);
});
}
else
{
// 单聊
const
msg
=
ctx
.
helper
.
parseMsg
(
'exchange'
,
payload
,
{
client
,
target
});
target
&&
nsp
.
sockets
[
target
].
emit
(
'msg'
,
msg
);
}
}
catch
(
error
)
{
}
catch
(
error
)
{
app
.
logger
.
error
(
error
);
app
.
logger
.
error
(
error
);
}
}
...
@@ -30,4 +36,4 @@ export default class NspController extends Controller {
...
@@ -30,4 +36,4 @@ export default class NspController extends Controller {
app
.
logger
.
error
(
error
);
app
.
logger
.
error
(
error
);
}
}
}
}
}
}
\ No newline at end of file
app/io/middleware/auth.ts
View file @
ab1f5e38
...
@@ -2,11 +2,7 @@ import { Context } from 'egg';
...
@@ -2,11 +2,7 @@ import { Context } from 'egg';
export
default
()
=>
{
export
default
()
=>
{
return
async
(
_ctx
:
Context
,
next
:
any
)
=>
{
return
async
(
_ctx
:
Context
,
next
:
any
)
=>
{
const
{
app
}
=
_ctx
;
const
nsp
=
app
.
io
.
of
(
'/'
)
as
any
;
nsp
.
emit
(
'updateList'
,
nsp
.
sockets
);
await
next
();
await
next
();
console
.
log
(
'disconnection!'
);
// 断开连接后,删除对应的 群聊信息
nsp
.
emit
(
'updateList'
,
nsp
.
sockets
);
};
};
};
};
app/io/middleware/packet.ts
View file @
ab1f5e38
...
@@ -2,8 +2,10 @@ import { Context } from 'egg';
...
@@ -2,8 +2,10 @@ import { Context } from 'egg';
export
default
()
=>
{
export
default
()
=>
{
return
async
(
ctx
:
Context
,
next
:
any
)
=>
{
return
async
(
ctx
:
Context
,
next
:
any
)
=>
{
ctx
.
socket
.
emit
(
'res'
,
'packet received!'
);
ctx
.
socket
.
emit
(
'res'
,
{
console
.
log
(
'packet:'
,
ctx
.
packet
);
time
:
new
Date
().
getTime
(),
success
:
'received'
,
});
await
next
();
await
next
();
};
};
};
};
app/router.ts
View file @
ab1f5e38
...
@@ -6,6 +6,10 @@ export default (app: Application) => {
...
@@ -6,6 +6,10 @@ export default (app: Application) => {
const
{
controller
,
router
}
=
app
;
const
{
controller
,
router
}
=
app
;
router
.
get
(
'/'
,
controller
.
home
.
index
);
router
.
get
(
'/'
,
controller
.
home
.
index
);
router
.
get
(
'/socket/queryRooms'
,
controller
.
socket
.
queryList
);
router
.
post
(
'/socket/createroom'
,
controller
.
socket
.
createRoom
);
router
.
post
(
'/socket/joinroom'
,
controller
.
socket
.
joinRoom
);
accountRouter
(
app
);
accountRouter
(
app
);
socketRouter
(
app
);
socketRouter
(
app
);
};
};
app/service/socket.ts
0 → 100644
View file @
ab1f5e38
import
{
Service
}
from
'egg'
;
/**
* Test Service
*/
export
default
class
Socket
extends
Service
{
public
async
createRoom
()
{
const
{
app
,
ctx
}
=
this
;
const
{
redis
}
=
app
;
const
{
request
}
=
ctx
;
const
body
=
request
.
body
;
const
{
groupId
,
groupName
,
socketId
}
=
body
;
const
group
=
await
ctx
.
helper
.
queryGroup
(
redis
,
groupId
);
if
(
group
)
{
return
{
data
:
{
groupId
,
groupName
,
},
code
:
1
,
msg
:
'房间已存在'
,
};
}
await
ctx
.
helper
.
cacheGroupId
(
redis
,
groupId
,
socketId
,
groupName
);
return
{
data
:
{
groupId
,
groupName
,
},
code
:
0
,
msg
:
'success'
,
};
}
public
async
joinRoom
()
{
const
{
app
,
ctx
}
=
this
;
const
{
redis
}
=
app
;
const
{
request
}
=
ctx
;
const
body
=
request
.
body
;
const
{
groupId
,
socketId
,
groupName
}
=
body
;
await
ctx
.
helper
.
cacheGroupId
(
redis
,
groupId
,
socketId
,
groupName
);
return
{
data
:
{
groupId
,
id
:
socketId
,
},
code
:
0
,
msg
:
'success'
,
};
}
public
async
queryRooms
()
{
const
{
app
}
=
this
;
const
{
redis
,
logger
}
=
app
;
const
data
=
await
redis
.
get
(
'gk_chat_room'
);
logger
.
info
(
'queryRooms: '
,
data
);
const
list
=
JSON
.
parse
(
data
||
'[]'
)
||
[];
return
{
data
:
list
.
map
((
itm
:
any
)
=>
({
groupId
:
itm
.
groupId
,
groupName
:
itm
.
groupName
})),
code
:
0
,
msg
:
''
,
};
}
}
app/socket-router.ts
View file @
ab1f5e38
...
@@ -2,6 +2,6 @@ import { Application } from 'egg';
...
@@ -2,6 +2,6 @@ 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
);
io
.
of
(
'/'
).
route
(
'chatList'
,
(
io
.
controller
as
any
).
nsp
.
chatList
);
};
};
typings/app/controller/index.d.ts
View file @
ab1f5e38
...
@@ -4,12 +4,12 @@
...
@@ -4,12 +4,12 @@
import
'egg'
;
import
'egg'
;
import
ExportAccount
from
'../../../app/controller/account'
;
import
ExportAccount
from
'../../../app/controller/account'
;
import
ExportHome
from
'../../../app/controller/home'
;
import
ExportHome
from
'../../../app/controller/home'
;
import
Export
News
from
'../../../app/controller/news
'
;
import
Export
Socket
from
'../../../app/controller/socket
'
;
declare
module
'egg'
{
declare
module
'egg'
{
interface
IController
{
interface
IController
{
account
:
ExportAccount
;
account
:
ExportAccount
;
home
:
ExportHome
;
home
:
ExportHome
;
news
:
ExportNews
;
socket
:
ExportSocket
;
}
}
}
}
typings/app/service/index.d.ts
View file @
ab1f5e38
...
@@ -8,10 +8,12 @@ type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>;
...
@@ -8,10 +8,12 @@ type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>;
type
AutoInstanceType
<
T
,
U
=
T
extends
CanExportFunc
?
T
:
T
extends
AnyFunc
?
ReturnType
<
T
>
:
T
>
=
U
extends
AnyClass
?
InstanceType
<
U
>
:
U
;
type
AutoInstanceType
<
T
,
U
=
T
extends
CanExportFunc
?
T
:
T
extends
AnyFunc
?
ReturnType
<
T
>
:
T
>
=
U
extends
AnyClass
?
InstanceType
<
U
>
:
U
;
import
ExportAccount
from
'../../../app/service/account'
;
import
ExportAccount
from
'../../../app/service/account'
;
import
ExportHome
from
'../../../app/service/home'
;
import
ExportHome
from
'../../../app/service/home'
;
import
ExportSocket
from
'../../../app/service/socket'
;
declare
module
'egg'
{
declare
module
'egg'
{
interface
IService
{
interface
IService
{
account
:
AutoInstanceType
<
typeof
ExportAccount
>
;
account
:
AutoInstanceType
<
typeof
ExportAccount
>
;
home
:
AutoInstanceType
<
typeof
ExportHome
>
;
home
:
AutoInstanceType
<
typeof
ExportHome
>
;
socket
:
AutoInstanceType
<
typeof
ExportSocket
>
;
}
}
}
}
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