Skip to content

Commit

Permalink
fix connect status in both client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyLaw committed Mar 12, 2020
1 parent 9293211 commit bf106ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Composer/packages/client/src/store/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SensitiveProperties } from '@bfc/shared';
import { Diagnostic, DiagnosticSeverity, LgTemplate, lgIndexer, luIndexer } from '@bfc/indexers';
import { ImportResolverDelegate } from 'botbuilder-lg';

import { ActionTypes, FileTypes } from '../../constants';
import { ActionTypes, FileTypes, BotStatus } from '../../constants';
import { DialogSetting, ReducerFunc } from '../types';
import { UserTokenPayload } from '../action/types';
import { getExtension, getFileName, getBaseName } from '../../utils';
Expand Down Expand Up @@ -47,6 +47,7 @@ const getProjectSuccess: ReducerFunc = (state, { response }) => {
state.dialogs = response.data.dialogs;
state.botEnvironment = response.data.botEnvironment || state.botEnvironment;
state.botName = response.data.botName;
state.botStatus = response.data.location === state.location ? state.botStatus : BotStatus.unConnected;
state.location = response.data.location;
state.lgFiles = response.data.lgFiles;
state.schemas = response.data.schemas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class CSharpBotConnector implements IBotConnector {
public status: BotStatus = BotStatus.NotConnected;
private endpoint: string;
static botRuntimes: { [key: string]: ChildProcess } = {};
constructor(endpoint: string) {
this.endpoint = endpoint;
constructor() {
this.endpoint = currentConfig.endpoint;
}

static stopAll = (signal: string) => {
Expand Down Expand Up @@ -170,11 +170,17 @@ export class CSharpBotConnector implements IBotConnector {
};

connect = async (_: BotEnvironments, __: string) => {
const originPort = urlParse(this.endpoint).port;
const port = await getPort({ host: 'localhost', port: parseInt(originPort || '3979') });
this.endpoint = `http://localhost:${port}`;
currentConfig.endpoint = this.endpoint;
return `http://localhost:${port}/api/messages`;
const port = urlParse(this.endpoint).port;
if (this.status === BotStatus.NotConnected) {
// check the port availability
const hostname = urlParse(this.endpoint).hostname;
const newPort = await getPort({ host: 'localhost', port: parseInt(port || '3979') });
this.endpoint = `http://${hostname}:${newPort}`;
this.status = BotStatus.Connected;
return `http://localhost:${newPort}/api/messages`;
} else {
return `http://localhost:${port}/api/messages`;
}
};

sync = async (config: DialogSetting) => {
Expand Down Expand Up @@ -215,6 +221,8 @@ export class CSharpBotConnector implements IBotConnector {
});
};
}

export const localConnector = new CSharpBotConnector();
const cleanup = (signal: NodeJS.Signals) => {
CSharpBotConnector.stopAll(signal);
process.exit(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { ISettingManager } from '../settings';
import { DefaultSettingManager } from '../settings/defaultSettingManager';
import { IBotConnector } from '../connector';
import { CSharpBotConnector } from '../connector/csharpBotConnector';
import { CSharpBotConnector, localConnector } from '../connector/csharpBotConnector';

import { IEnvironmentConfig, IEnvironment } from '.';

Expand All @@ -18,7 +18,7 @@ export class DefaultEnvironment implements IEnvironment {
public constructor(config: IEnvironmentConfig) {
this.config = config;
this.settingManager = new DefaultSettingManager(this.config.basePath);
this.botConnector = new CSharpBotConnector(this.config.endpoint);
this.botConnector = localConnector;
}

public getEnvironmentName(_: string): string | undefined {
Expand Down

0 comments on commit bf106ea

Please sign in to comment.