Skip to content

Commit

Permalink
Use socket.io for Temporal -> Svelte updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
robholland committed Aug 27, 2024
1 parent ef880cd commit 92869ae
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 45 deletions.
100 changes: 87 additions & 13 deletions game/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion game/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@temporalio/client": "^1.10.2",
"@temporalio/worker": "^1.10.2",
"@temporalio/workflow": "^1.10.2",
"undici": "^6.19.8",
"socket.io-client": "^4.7.5",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
31 changes: 4 additions & 27 deletions game/src/activities.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import { Snake, Round } from './workflows';
import { request, Agent } from 'undici';
import { io } from 'socket.io-client';

const agent = new Agent({
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10
});
const socket = io('http://localhost:5173');

export async function snakeWork(durationMs: number) {
// sleep for duration
await new Promise((resolve) => setTimeout(resolve, durationMs));
}

export async function snakeMovedNotification(snake: Snake) {
await request(`http://localhost:5173/api/signal`, {
dispatcher: agent,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'snakeMoved',
snake: snake,
})
});
socket.emit('snakeMoved', snake);
}

export async function roundUpdateNotification(round: Round) {
await request(`http://localhost:5173/api/game`, {
dispatcher: agent,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'roundUpdate',
round: { ...round, stale: undefined },
})
});
socket.emit('roundUpdate', { ...round, stale: undefined });
}
3 changes: 1 addition & 2 deletions snakes/src/routes/[id]/round/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
function connectSocket() {
socket = io();
socket.on('snakeMoved', (newData) => {
const newSnake = newData.snake;
socket.on('snakeMoved', (newSnake) => {
if (newSnake.id === 'red-0') {
SnakeRed1.redraw(newSnake);
} else if (newSnake.id === 'red-1') {
Expand Down
9 changes: 7 additions & 2 deletions snakes/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ const webSocketServer = {

const io = new Server(server.httpServer);
globalThis.io = io;
io.on('connection', (connection) => {
connection.emit('eventFromServer', '✅ Connected');
io.on('connection', (socket) => {
socket.on('snakeMoved', (snake) => {
io.emit('snakeMoved', snake);
});
socket.on('roundUpdate', (round) => {
io.emit('roundUpdate', round);
});
});
}
};
Expand Down

0 comments on commit 92869ae

Please sign in to comment.