microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
acf08bc211b251f5aae90239291dbef21894989c

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/extensionMessaging.ts

60lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5import * as http from "http";
6
7/**
8 * Message server parameters.
9 */
10export let ServerDefaultParams = {
11 PORT: 8099,
12 HOST: "127.0.0.1"
13};
14
15/**
16 * Defines the messages sent to the extension.
17 * Add new messages to this enum.
18 */
19export enum ExtensionIncomingMessage {
20 START_PACKAGER,
21 STOP_PACKAGER
22}
23
24/**
25 * Sends messages to the extension.
26 */
27export class ExtensionMessageSender {
28
29 public sendMessage(message: ExtensionIncomingMessage, args?: any, port?: number): Q.Promise<any> {
30 let deferred = Q.defer<any>();
31
32 let options = {
33 host: ServerDefaultParams.HOST,
34 port: port || ServerDefaultParams.PORT,
35 path: "/" + ExtensionIncomingMessage[message],
36 method: "POST",
37 headers: { "Content-Type": "application/json" }
38 };
39
40 let responseCallback = (response: http.IncomingMessage) => {
41 let body = "";
42
43 response.on("data", function(data: any) {
44 body += data;
45 });
46
47 response.on("end", function() {
48 let responseBody: any = JSON.parse(body);
49 console.log("Response: " + body);
50 deferred.resolve(responseBody);
51 });
52 };
53
54 let postRequest = http.request(options, responseCallback);
55 postRequest.write(JSON.stringify(args));
56 postRequest.end();
57
58 return deferred.promise;
59 }
60}