Skip to content

Instantly share code, notes, and snippets.

View Srushtika's full-sized avatar
:octocat:

Srushtika Neelakantam Srushtika

:octocat:
View GitHub Profile
@Srushtika
Srushtika / client.js
Created November 16, 2018 14:38
WebSockets server tutorial
ws.addEventListener('message', event => {
// The `event` object is a typical DOM event object, and the message data sent
// by the server is stored in the `data` property
console.log('Received:', event.data);
});
@Srushtika
Srushtika / client.js
Created November 16, 2018 14:41
WebSockets server tutorial
const ws = new WebSocket('ws://localhost:3210', ['json', 'xml']);
ws.addEventListener('open', () => {
const data = { message: 'Hello from the client!' }
const json = JSON.stringify(data);
ws.send(json);
});
ws.addEventListener('message', event => {
const data = JSON.parse(event.data);
console.log(data);
});
@Srushtika
Srushtika / server.js
Created November 16, 2018 14:45
WebSockets server tutorial
socket.on('data', buffer => {
const message = parseMessage(buffer);
if (message) {
// For our convenience, so we can see what the client sent
console.log(message);
// We'll just send a hardcoded message in this example
socket.write(constructReply({ message: 'Hello from the server!' }));
} else if (message === null) {
console.log('WebSocket connection closed by the client.');
}
@Srushtika
Srushtika / server.js
Created November 16, 2018 14:49
WebSockets server tutorial
let maskingKey;
if (isMasked) {
maskingKey = buffer.readUInt32BE(currentOffset);
currentOffset += 4;
}
@Srushtika
Srushtika / server.js
Created November 16, 2018 14:56
WebSockets server tutorial
socket.on('data', buffer => {
const message = parseMessage(buffer);
if (message) {
// For our convenience, so we can see what the client sent
console.log(message);
// We'll just send a hardcoded message in this example
socket.write(constructReply({ message: 'Hello from the server!' }));
} else if (message === null) {
console.log('WebSocket connection closed by the client.');
}
curl -X POST https://rest.ably.io/channels/fox-gin/messages \
-u '6QdTiA.O9vBCw:8glD3fRTNhSEVJC5' \
--data 'name=greeting&data=hello'
#!/bin/bash
if(! (gem list -i iStats))
then
gem install iStats
exit 0
fi
myDeviceTemp=$(istats | awk '{if($1=="CPU") { print "CPUtemp:" $3 }; } END { }')
myBatteryTemp=$(istats | awk '{if($1=="Battery") { print "BatteryTemp:" $3 }; } END { }')
@Srushtika
Srushtika / server.js
Last active October 13, 2019 16:52
WebSockets server tutorial
function constructReply (data) {
// Convert the data to JSON and copy it into a buffer
const json = JSON.stringify(data)
const jsonByteLength = Buffer.byteLength(json);
// Note: we're not supporting > 65535 byte payloads at this stage
const lengthByteCount = jsonByteLength < 126 ? 0 : 2;
const payloadLength = lengthByteCount === 0 ? jsonByteLength : 126;
const buffer = Buffer.alloc(2 + lengthByteCount + jsonByteLength);
// Write out the first byte, using opcode `1` to indicate that the message
// payload contains text data
@Srushtika
Srushtika / server.js
Last active October 13, 2019 16:55
WebSockets server tutorial
function parseMessage (buffer) {
const firstByte = buffer.readUInt8(0);
const isFinalFrame = Boolean((firstByte >>> 7) & 0×1);
const [reserved1, reserved2, reserved3] = [ Boolean((firstByte >>> 6) & 0×1), Boolean((firstByte >>> 5) & 0×1), Boolean((firstByte >>> 4) & 0×1) ];
const opCode = firstByte & 0xF;
// We can return null to signify that this is a connection termination frame
if (opCode === 0×8)
return null;
// We only care about text frames from this point onward
if (opCode !== 0×1)
[{
id: <string>,
clientId: <optional, string>
platform: <string>
formFactor: <string>,
metadata: <object>,
updateToken: <string>,
push: {
recipient: {
transportType: <string>,