You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
38 lines
1.0 KiB
import { Injectable } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { SSE } from 'sse.js';
|
|
import { Observable, Subject } from 'rxjs';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class CodeExecutionService {
|
|
private apiUrl = 'http://localhost:3000/run';
|
|
private resultSubject = new Subject<string>();
|
|
|
|
constructor(private http: HttpClient) {}
|
|
|
|
executeCode(code: string, language: string) {
|
|
const sse = new SSE(this.apiUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'text/event-stream',
|
|
},
|
|
payload: JSON.stringify({ code, language }),
|
|
});
|
|
|
|
sse.addEventListener('message', (event: MessageEvent<string>) => {
|
|
const result = event.data;
|
|
|
|
// Émettre le résultat à tous les abonnés
|
|
const text = decodeURIComponent(result.replace(/%00/g, ''));
|
|
+ this.resultSubject.next(text);
|
|
});
|
|
}
|
|
|
|
getResult(): Observable<string> {
|
|
return this.resultSubject.asObservable();
|
|
}
|
|
}
|