Skip to content

Commit 192f9bc

Browse files
Merge pull request #131 from tve/main
cors: support cross-site access to socket.io
2 parents 65310e0 + 792494a commit 192f9bc

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

nodes/uibuilder.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,21 @@ module.exports = function(/** @type Red */ RED) {
251251
const uib_socketPath = tilib.urlJoin(httpNodeRoot, uib.moduleName, 'vendor', 'socket.io')
252252

253253
log.trace('[uibuilder:Module] Socket.IO initialisation - Socket Path=', uib_socketPath )
254-
var io = socketio.listen(RED.server, {'path': uib_socketPath}) // listen === attach
254+
let ioOptions = {
255+
'path': uib_socketPath,
256+
// for CORS need to handle preflight request explicitly 'cause there's an
257+
// Allow-Headers:X-ClientId in there. see https://socket.io/docs/v2/handling-cors/
258+
handlePreflightRequest: (req, res) => {
259+
res.writeHead(204, {
260+
"Access-Control-Allow-Origin": req.headers["origin"],
261+
"Access-Control-Allow-Methods": "GET,POST",
262+
"Access-Control-Allow-Headers": "X-ClientId",
263+
"Access-Control-Allow-Credentials": true,
264+
});
265+
res.end();
266+
},
267+
}
268+
var io = socketio.listen(RED.server, ioOptions) // listen === attach
255269
// @ts-ignore
256270
io.set('transports', ['polling', 'websocket'])
257271

0 commit comments

Comments
 (0)