|
|
@ -1,16 +1,20 @@
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
|
|
|
|
import { SSE } from 'sse.js';
|
|
|
|
import { SSE } from 'sse.js';
|
|
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ExecutionMessage = {
|
|
|
|
|
|
|
|
type: 'stdout' | 'stderr' | 'exit',
|
|
|
|
|
|
|
|
text: string
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root',
|
|
|
|
providedIn: 'root',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
export class CodeExecutionService {
|
|
|
|
export class CodeExecutionService {
|
|
|
|
private apiUrl = 'http://localhost:3000/run';
|
|
|
|
private apiUrl = 'http://localhost:3000/run';
|
|
|
|
private resultSubject = new Subject<string>();
|
|
|
|
private resultSubject = new Subject<ExecutionMessage>();
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
|
|
|
|
executeCode(code: string, language: string) {
|
|
|
|
executeCode(code: string, language: string) {
|
|
|
|
const sse = new SSE(this.apiUrl, {
|
|
|
|
const sse = new SSE(this.apiUrl, {
|
|
|
@ -25,13 +29,17 @@ export class CodeExecutionService {
|
|
|
|
sse.addEventListener('message', (event: MessageEvent<string>) => {
|
|
|
|
sse.addEventListener('message', (event: MessageEvent<string>) => {
|
|
|
|
const result = event.data;
|
|
|
|
const result = event.data;
|
|
|
|
|
|
|
|
|
|
|
|
// Émettre le résultat à tous les abonnés
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
const type = event.id;
|
|
|
|
const text = decodeURIComponent(result.replace(/%00/g, ''));
|
|
|
|
const text = decodeURIComponent(result.replace(/%00/g, ''));
|
|
|
|
this.resultSubject.next(text);
|
|
|
|
if (type === 'end') {
|
|
|
|
|
|
|
|
sse.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.resultSubject.next({ type, text });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getResult(): Observable<string> {
|
|
|
|
getResult(): Observable<ExecutionMessage> {
|
|
|
|
return this.resultSubject.asObservable();
|
|
|
|
return this.resultSubject.asObservable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|