-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pierre Cauchois
committed
Jul 25, 2018
1 parent
1d011eb
commit ed06a12
Showing
2 changed files
with
51 additions
and
1 deletion.
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
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,50 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
'use strict'; | ||
|
||
var websocket = require('websocket-stream') | ||
var ServiceClient = require('azure-iothub').Client; | ||
var net = require('net'); | ||
|
||
var streamInit = { | ||
streamName: 'TestStream', | ||
contentType: 'text/plain', | ||
contentEncoding: 'utf-8', | ||
connectTimeoutInSeconds: 30, | ||
responseTimeoutInSeconds: 30, | ||
payload: undefined | ||
} | ||
|
||
var client = ServiceClient.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING); | ||
|
||
console.log('initiating stream'); | ||
client.initiateStream(process.env.STREAMING_TARGET_DEVICE, streamInit, function(err, result) { | ||
if (err) { | ||
console.error(err.toString()); | ||
process.exit(-1); | ||
} else { | ||
console.log(JSON.stringify(result, null, 2)); | ||
|
||
var ws = websocket(result.uri, { headers: { 'Authorization': 'Bearer ' + result.authorizationToken} }); | ||
console.log('Got websocket - creating local server on port ' + process.env.PROXY_PORT); | ||
var proxyServer = net.createServer(function (socket) { | ||
socket.on('end', function () { | ||
console.log('client disconnected'); | ||
process.exit(0); | ||
}) | ||
|
||
socket.pipe(ws); | ||
ws.pipe(socket); | ||
}); | ||
|
||
proxyServer.on('error', function (err) { | ||
console.error('error on the proxy server socket: ' + err.toString()); | ||
process.exit(-1); | ||
}) | ||
|
||
proxyServer.listen(process.env.PROXY_PORT, function () { | ||
console.log('listening on port: ' + process.env.PROXY_PORT + '...'); | ||
}) | ||
} | ||
}); |