This repository was archived by the owner on Dec 2, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +34
-5
lines changed Expand file tree Collapse file tree 4 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ $(function onload() {
43
43
txtUsername . val ( '' ) ;
44
44
chatEmpty ( ) ;
45
45
chatWriteLine ( `You are connected! (${ s } )` ) ;
46
+ txtMessage . focus ( ) ;
46
47
} else {
47
48
alert ( r ) ;
48
49
txtUsername . focus ( ) ;
@@ -128,6 +129,12 @@ $(function onload() {
128
129
) ;
129
130
} ) ;
130
131
132
+ txtUsername . on ( 'keypress' , ( e ) => {
133
+ if ( e . which === 13 ) {
134
+ btnJoin . click ( ) ;
135
+ }
136
+ } ) ;
137
+
131
138
txtMessage . on ( 'keypress' , ( e ) => {
132
139
if ( e . which === 13 ) {
133
140
btnSend . click ( ) ;
Original file line number Diff line number Diff line change
1
+ export const UPPERCASE_USERNAMES : boolean = false ;
Original file line number Diff line number Diff line change @@ -41,6 +41,17 @@ export function FindConnById(pId: string): Promise<Connection | null> {
41
41
} ) ;
42
42
}
43
43
44
+ export function FindConnByName ( pName : string ) : Promise < Connection | null > {
45
+ return new Promise ( ( resolve ) => {
46
+ for ( const [ _Id , _Conn ] of Connections . entries ( ) ) {
47
+ if ( _Conn . name . toUpperCase ( ) === pName . toUpperCase ( ) ) {
48
+ return resolve ( _Conn ) ;
49
+ }
50
+ }
51
+ resolve ( null ) ;
52
+ } ) ;
53
+ }
54
+
44
55
export function RemoveConnById ( pId : string ) : Promise < Boolean > {
45
56
return new Promise ( ( resolve ) => {
46
57
const deleted = Connections . delete ( pId ) ;
Original file line number Diff line number Diff line change @@ -10,8 +10,11 @@ import {
10
10
RemoveConnById ,
11
11
CheckConnById ,
12
12
ConnInfo ,
13
+ FindConnByName ,
13
14
} from './Connections.ts' ;
14
15
16
+ import { UPPERCASE_USERNAMES } from './Configuration.ts' ;
17
+
15
18
/**
16
19
* h: Handler
17
20
* s: Sender
@@ -40,14 +43,21 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
40
43
const objEvent : WSMessage = JSON . parse ( event ) ;
41
44
switch ( objEvent . h ) {
42
45
case 'join' : {
43
- const _name = objEvent . d ;
44
- if ( / ^ [ a - z A - Z 0 - 9 ] + $ / i. test ( _name ) ) {
46
+ const _name = UPPERCASE_USERNAMES
47
+ ? objEvent . d . toUpperCase ( )
48
+ : objEvent . d ;
49
+ if ( ! / ^ [ a - z A - Z 0 - 9 ] + $ / i. test ( _name ) ) {
50
+ await RespondJoin ( _connInfo , 'Invalid username' ) ;
51
+ } else if ( await FindConnByName ( _name ) ) {
52
+ await RespondJoin (
53
+ _connInfo ,
54
+ 'Username already in use'
55
+ ) ;
56
+ } else {
45
57
_conn . state = true ;
46
- _conn . name = objEvent . d ;
58
+ _conn . name = _name ;
47
59
await BroadcastJoin ( _connInfo ) ;
48
60
await RespondJoin ( _connInfo , 'OK' ) ;
49
- } else {
50
- await RespondJoin ( _connInfo , 'Invalid username' ) ;
51
61
}
52
62
break ;
53
63
}
You can’t perform that action at this time.
0 commit comments