-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a WebSockets API behind CloudFront
- Loading branch information
1 parent
5a94759
commit 0ce5876
Showing
7 changed files
with
246 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const DynamoDB = require("aws-sdk/clients/dynamodb"); | ||
const schema = require("../schema"); | ||
|
||
exports.handler = async function (event, context, callback) { | ||
console.log("connect handler called"); | ||
console.log("event:"); | ||
console.log(event); | ||
console.log("context:"); | ||
console.log(context); | ||
const db = new DynamoDB.DocumentClient(); | ||
const now = new Date(); | ||
var putParams = { | ||
TableName: process.env.TABLE_NAME, | ||
Item: { | ||
[schema.PARTITION_KEY]: `CHECK?PARAMS`, // need to see if event has params | ||
[schema.SORT_KEY]: | ||
`${schema.CONNECT}` + | ||
`${schema.SEPARATOR}` + | ||
`${event.requestContext.connectionId}`, | ||
[schema.CONNECTION_ID]: event.requestContext.connectionId, | ||
[schema.DATE]: now.toISOString(), | ||
[schema.MS]: now.getTime() | ||
}, | ||
}; | ||
|
||
try { | ||
// Insert incoming connection id in the WebSocket | ||
await db.put(putParams).promise(); | ||
|
||
return { | ||
statusCode: 200, | ||
body: "Connected", | ||
}; | ||
} catch (e) { | ||
console.error("connect error!", e); | ||
return { | ||
statusCode: 501, | ||
body: "Failed to connect: " + JSON.stringify(e), | ||
}; | ||
} | ||
}; |
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,13 @@ | ||
const schema = require("../schema"); | ||
|
||
exports.handler = async function (event, context, callback) { | ||
console.log("default handler called"); | ||
console.log("event:"); | ||
console.log(event); | ||
console.log("context:"); | ||
console.log(context); | ||
return { | ||
statusCode: 200, | ||
body: "defaulted", | ||
}; | ||
}; |
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,41 @@ | ||
const DynamoDB = require("aws-sdk/clients/dynamodb"); | ||
const schema = require("../schema"); | ||
|
||
exports.handler = async function (event, context, callback) { | ||
console.log("disconnect handler called"); | ||
console.log("event:"); | ||
console.log(event); | ||
console.log("context:"); | ||
console.log(context); | ||
const db = new DynamoDB.DocumentClient(); | ||
const now = new Date(); | ||
var putParams = { | ||
TableName: process.env.TABLE_NAME, | ||
Item: { | ||
[schema.PARTITION_KEY]: `CHECK?PARAMS`, // need to see if event has params | ||
[schema.SORT_KEY]: | ||
`${schema.DISCONNECT}` + | ||
`${schema.SEPARATOR}` + | ||
`${event.requestContext.connectionId}`, | ||
[schema.CONNECTION_ID]: event.requestContext.connectionId, | ||
[schema.DATE]: now.toISOString(), | ||
[schema.MS]: now.getTime() | ||
}, | ||
}; | ||
|
||
try { | ||
// Insert incoming connection id in the WebSocket | ||
await db.put(putParams).promise(); | ||
|
||
return { | ||
statusCode: 200, | ||
body: "Disconnected", | ||
}; | ||
} catch (e) { | ||
console.error("disconnect error!", e); | ||
return { | ||
statusCode: 501, | ||
body: "Failed to disconnect: " + JSON.stringify(e), | ||
}; | ||
} | ||
}; |
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
Oops, something went wrong.