-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(socketio): Update documentation and example with SocketUseBefore
- Loading branch information
Showing
27 changed files
with
431 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {ServerLoader, ServerSettings} from "@tsed/common"; | ||
import "@tsed/socketio"; // import socketio Ts.ED module | ||
|
||
@ServerSettings({ | ||
rootDir: __dirname, | ||
socketIO: { | ||
// ... see configuration | ||
} | ||
}) | ||
export class Server extends ServerLoader { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Args, Input, Namespace, Socket, SocketService} from "@tsed/socketio"; | ||
|
||
@SocketService("/my-namespace") | ||
export class MySocketService { | ||
@Input("eventName") | ||
myMethod(@Args(0) userName: string, @Socket socket: Socket, @Namespace nsp: Namespace) { | ||
console.log(userName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Socket, SocketErr, SocketEventName, SocketMiddlewareError} from "@tsed/socketio"; | ||
|
||
@SocketMiddlewareError() | ||
export class ErrorHandlerSocketMiddleware { | ||
async use(@SocketEventName eventName: string, @SocketErr err: any, @Socket socket: Socket) { | ||
console.error(err); | ||
socket.emit("error", {message: "An error has occured"}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {Args, Emit, Input, Socket, SocketService} from "@tsed/socketio"; | ||
|
||
@SocketService("/my-namespace") | ||
export class MySocketService { | ||
@Input("eventName") | ||
@Emit("responseEventName") // or Broadcast or BroadcastOthers | ||
async myMethod(@Args(0) userName: string, @Socket socket: Socket) { | ||
return "Message " + userName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {Controller, Get} from "@tsed/common"; | ||
import {MySocketService} from "../services/MySocketService"; | ||
|
||
@Controller("/") | ||
export class MyCtrl { | ||
|
||
constructor(private mySocketService: MySocketService) { | ||
|
||
} | ||
|
||
@Get("/allo") | ||
allo() { | ||
this.mySocketService.helloAll(); | ||
|
||
return "is sent"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {Namespace, SocketService} from "@tsed/socketio"; | ||
|
||
@SocketService() | ||
export class MySocketService { | ||
@Namespace nsp: Namespace; | ||
|
||
helloAll() { | ||
this.nsp.emit("hi", "everyone!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {IO, Nsp, Socket, SocketService, SocketSession} from "@tsed/socketio"; | ||
import * as SocketIO from "socket.io"; | ||
|
||
@SocketService("/my-namespace") | ||
export class MySocketService { | ||
|
||
@Nsp nsp: SocketIO.Namespace; | ||
|
||
@Nsp("/my-other-namespace") | ||
nspOther: SocketIO.Namespace; // communication between two namespace | ||
|
||
|
||
constructor(@IO private io: SocketIO.Server) { | ||
} | ||
|
||
/** | ||
* Triggered the namespace is created | ||
*/ | ||
$onNamespaceInit(nsp: SocketIO.Namespace) { | ||
|
||
} | ||
|
||
/** | ||
* Triggered when a new client connects to the Namespace. | ||
*/ | ||
$onConnection(@Socket socket: SocketIO.Socket, @SocketSession session: SocketSession) { | ||
|
||
} | ||
|
||
/** | ||
* Triggered when a client disconnects from the Namespace. | ||
*/ | ||
$onDisconnect(@Socket socket: SocketIO.Socket) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {Args, Emit, Input, SocketService, SocketSession} from "@tsed/socketio"; | ||
|
||
@SocketService("/my-namespace") | ||
export class MySocketService { | ||
@Input("eventName") | ||
@Emit("responseEventName") // or Broadcast or BroadcastOthers | ||
async myMethod(@Args(0) userName: string, @SocketSession session: SocketSession) { | ||
|
||
const user = session.get("user") || {}; | ||
user.name = userName; | ||
|
||
session.set("user", user); | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {ConverterService} from "@tsed/common"; | ||
import {Args, SocketMiddleware} from "@tsed/socketio"; | ||
import {User} from "../models/User"; | ||
|
||
@SocketMiddleware() | ||
export class UserConverterSocketMiddleware { | ||
constructor(private converterService: ConverterService) { | ||
} | ||
|
||
async use(@Args() args: any[]) { | ||
|
||
let [user] = args; | ||
// update Arguments | ||
user = this.converterService.deserialize(user, User); | ||
|
||
return [user]; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
docs/tutorials/snippets/socketio/socket-use-middleware2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {SocketService, SocketUseAfter, SocketUseBefore, Emit, Input, Args} from "@tsed/socketio"; | ||
import {UserConverterSocketMiddleware, ErrorHandlerSocketMiddleware} from "../middlewares"; | ||
import {User} from "../models/User"; | ||
|
||
@SocketService("/my-namespace") | ||
@SocketUseBefore(UserConverterSocketMiddleware) // global version | ||
@SocketUseAfter(ErrorHandlerSocketMiddleware) | ||
export class MySocketService { | ||
|
||
@Input("eventName") | ||
@Emit("responseEventName") // or Broadcast or BroadcastOthers | ||
@SocketUseBefore(UserConverterSocketMiddleware) | ||
@SocketUseAfter(ErrorHandlerSocketMiddleware) | ||
async myMethod(@Args(0) user: User) { | ||
|
||
console.log(user); | ||
|
||
return user; | ||
} | ||
} |
Oops, something went wrong.