Adding longpolling
This commit is contained in:
parent
29d859b383
commit
181053e876
|
|
@ -7,7 +7,7 @@
|
|||
<script>
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
let connectButton = document.getElementById('connect');
|
||||
let connection = new RpcConnection(`http://${document.location.host}/hubs`);
|
||||
let connection = new RpcConnection(`http://${document.location.host}/hubs`, 'formatType=json&format=text');
|
||||
connection.on('Send',(msg) => { addLine(msg); });
|
||||
let isConnected = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,13 +4,18 @@ class HttpClient {
|
|||
}
|
||||
|
||||
post(url: string, content: string): Promise<string> {
|
||||
return this.xhr("POST", url);
|
||||
return this.xhr("POST", url, content);
|
||||
}
|
||||
|
||||
private xhr(method: string, url: string, content?: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open(method, url, true);
|
||||
|
||||
if (method === "POST" && content != null) {
|
||||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
}
|
||||
|
||||
xhr.send(content);
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
|
|
|
|||
|
|
@ -13,10 +13,16 @@ class LongPollingTransport implements ITransport {
|
|||
}
|
||||
|
||||
connect(url: string, queryString: string): Promise<void> {
|
||||
this.queryString = queryString || "";
|
||||
this.url = url || "";
|
||||
|
||||
// TODO: resolve promise on open sending? + reject on error
|
||||
this.poll(url + "/poll?" + this.queryString)
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private poll(url: string): void {
|
||||
//TODO: timeout
|
||||
this.pollXhr.open("GET", url, true);
|
||||
this.pollXhr.send();
|
||||
this.pollXhr.onload = () => {
|
||||
|
|
@ -46,7 +52,7 @@ class LongPollingTransport implements ITransport {
|
|||
}
|
||||
|
||||
send(data: string): Promise<void> {
|
||||
return new HttpClient().post(this.url + "/poll/send?" + this.queryString, data);
|
||||
return new HttpClient().post(this.url + "/send?" + this.queryString, data);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
|
|
|
|||
|
|
@ -52,9 +52,9 @@ class RpcConnection {
|
|||
return new Promise((resolve, reject) => {
|
||||
new HttpClient().get(this.url + "/getid?" + this.queryString)
|
||||
.then(id => {
|
||||
this.transport = new WebSocketTransport(data => this.messageReceived(data));
|
||||
//this.transport = new LongPollingTransport(data => this.messageReceived(data));
|
||||
return this.transport.connect(this.url, `id=${id}`);
|
||||
// this.transport = new WebSocketTransport(data => this.messageReceived(data));
|
||||
this.transport = new LongPollingTransport(data => this.messageReceived(data));
|
||||
return this.transport.connect(this.url, `id=${id}&${this.queryString}`);
|
||||
})
|
||||
.then(() => {
|
||||
resolve();
|
||||
|
|
|
|||
|
|
@ -75,6 +75,9 @@ namespace Microsoft.AspNetCore.Sockets
|
|||
{
|
||||
bool isNewConnection;
|
||||
var state = GetOrCreateConnection(context, out isNewConnection);
|
||||
// TODO: this is wrong. + how does the user add their own metadata based on HttpContext
|
||||
var formatType = (string)context.Request.Query["formatType"];
|
||||
state.Connection.Metadata["formatType"] = string.IsNullOrEmpty(formatType) ? "json" : formatType;
|
||||
|
||||
// Mark the connection as active
|
||||
state.Active = true;
|
||||
|
|
|
|||
Loading…
Reference in New Issue