Skip to content

Commit

Permalink
Add service proxy sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Cauchois committed Jul 25, 2018
1 parent 1d011eb commit ed06a12
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion service/samples/c2d_tcp_streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var streamInit = {
var client = ServiceClient.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING);

console.log('initiating stream');
client.initiateStream('<DEVICE_ID>', streamInit, function(err, result) {
client.initiateStream(process.env.STREAMING_TARGET_DEVICE, streamInit, function(err, result) {
if (err) {
console.error(err.toString());
process.exit(-1);
Expand Down
50 changes: 50 additions & 0 deletions service/samples/tcp_streaming_proxy.js
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 + '...');
})
}
});

0 comments on commit ed06a12

Please sign in to comment.