Created
April 9, 2015 13:43
-
-
Save MrYsLab/8c45a21c32d3c9edaaee to your computer and use it in GitHub Desktop.
Scratch wss server example
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
/* I used this web page as a basis: http://www.chovy.com/web-development/self-signed-certs-with-secure-websockets-in-node-js/ | |
1. I created the key and cert using the instructions. | |
2. I created a modified secure websocket server - included below | |
3. I created a simplified Scratch extension - included below. | |
4. I added the cert to firefox using the instructions in the article for Chrome. This worked | |
5. I tried to add the cert to chrome by accessing https://0.0.0.0:1234, but this brought me to a search page. | |
6. I then created a pkc12 cert and used the settings to add it to chrome, but could not get a wss connection. | |
Bottom line - I got Firefox to work, but not chrome | |
*/ | |
/** | |
* A test web socket server | |
* Created by Alan Yorinks on 4/9/15. | |
*/ | |
var https = require('https'); | |
var fs = require('fs'); | |
var privateKey = fs.readFileSync('key.pem', 'utf8'); | |
var certificate = fs.readFileSync('cert.pem', 'utf8'); | |
var credentials = {key: privateKey, cert: certificate}; | |
var httpsServer = https.createServer(credentials, function (request, response) { | |
console.log("Req = " + request); | |
// We are not processing any HTTP, so this is an empty function. 'server' is a wrapper for the | |
// WebSocketServer we are going to create below. | |
}); | |
httpsServer.listen(1234); | |
var WebSocketServer = require('ws').Server; | |
var wss = new WebSocketServer({ | |
server: httpsServer | |
}); | |
wss.on('connection', function connection(ws) { | |
ws.on('message', function incoming(message) { | |
console.log('received: %s', message); | |
}); | |
ws.send('something'); | |
}); | |
/* This is the accompanying Scratch test extention */ | |
new (function () { | |
var ext = this; | |
var boardStatus = 0; | |
/******************************* | |
**** Websocket support **** | |
*******************************/ | |
var socket = new WebSocket('wss://' + '0.0.0.0' + ':' + 1234); | |
socket.binaryType = "arraybuffer"; | |
// attach an onopen handler to this socket. This message is sent by a servers websocket | |
socket.onopen = function (event) { | |
console.log("onopen message received"); | |
// change the board status to green with the first board added, since we don"t know ahead of time | |
// how many boards are attached | |
boardStatus = 2; | |
beenOpened = true; | |
}; | |
socket.onclose = function (event) { | |
console.log("on close received"); | |
boardStatus = 1; | |
//if (beenOpened == false) { | |
/// alert("Restart the server and then reload the browswer"); | |
//} | |
}; | |
// // Process messages received from the server associated with this socket. | |
socket.onmessage = function (message) { | |
sensor_data = message.data; | |
console.log(sensor_data); | |
}; | |
/*****************************************************************************************************/ | |
/*********************************** Scratch Program Block Handlers, ******************************/ | |
/*****************************************************************************************************/ | |
// Associate a handler for each block described in the blocks section below | |
/********************* asynchronous messages from scratch ****************************************/ | |
// Cleanup function when the extension is unloaded | |
ext._shutdown = function () { | |
boardStatus = 0; | |
var msg = "reset_esplora"; | |
socket.send(msg); | |
}; | |
// Status reporting code - part of boilerplate provided by Scratch | |
// Set the "LED" on the Scratch Editor | |
ext._getStatus = function () { | |
return { | |
status: boardStatus, | |
msg: "Ready" | |
}; | |
}; | |
/******************************* | |
**** Command Block Handlers **** | |
*******************************/ | |
ext.board_led = function (state) { | |
var msg = "board_led/" + state; | |
socket.send(msg); | |
}; | |
// Block and block menu descriptions | |
var descriptor = { | |
blocks: [ | |
[" ", "Yellow LED %m.off_on", "board_led", "Off"] | |
], | |
menus: { | |
off_on: ["Off", "On"] | |
}, | |
url: "http://mryslab.blogspot.com/" | |
}; | |
// Register the extension | |
ScratchExtensions.register("esp4s_v001_Feb_27_2015", descriptor, ext); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm the guy who wrote the article. I believe you have to go to port 443 first and accept the cert before you try to use it over sockets.