Adding longpolling

This commit is contained in:
moozzyk 2016-10-28 15:47:46 -07:00
parent 29d859b383
commit 181053e876
5 changed files with 20 additions and 6 deletions

View File

@ -7,7 +7,7 @@
<script> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
let connectButton = document.getElementById('connect'); 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); }); connection.on('Send',(msg) => { addLine(msg); });
let isConnected = false; let isConnected = false;

View File

@ -4,13 +4,18 @@ class HttpClient {
} }
post(url: string, content: string): Promise<string> { 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> { private xhr(method: string, url: string, content?: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open(method, url, true); xhr.open(method, url, true);
if (method === "POST" && content != null) {
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
xhr.send(content); xhr.send(content);
xhr.onload = () => { xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) { if (xhr.status >= 200 && xhr.status < 300) {

View File

@ -13,10 +13,16 @@ class LongPollingTransport implements ITransport {
} }
connect(url: string, queryString: string): Promise<void> { 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(); return Promise.resolve();
} }
private poll(url: string): void { private poll(url: string): void {
//TODO: timeout
this.pollXhr.open("GET", url, true); this.pollXhr.open("GET", url, true);
this.pollXhr.send(); this.pollXhr.send();
this.pollXhr.onload = () => { this.pollXhr.onload = () => {
@ -46,7 +52,7 @@ class LongPollingTransport implements ITransport {
} }
send(data: string): Promise<void> { 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 { stop(): void {

View File

@ -52,9 +52,9 @@ class RpcConnection {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
new HttpClient().get(this.url + "/getid?" + this.queryString) new HttpClient().get(this.url + "/getid?" + this.queryString)
.then(id => { .then(id => {
this.transport = new WebSocketTransport(data => this.messageReceived(data)); // this.transport = new WebSocketTransport(data => this.messageReceived(data));
//this.transport = new LongPollingTransport(data => this.messageReceived(data)); this.transport = new LongPollingTransport(data => this.messageReceived(data));
return this.transport.connect(this.url, `id=${id}`); return this.transport.connect(this.url, `id=${id}&${this.queryString}`);
}) })
.then(() => { .then(() => {
resolve(); resolve();

View File

@ -75,6 +75,9 @@ namespace Microsoft.AspNetCore.Sockets
{ {
bool isNewConnection; bool isNewConnection;
var state = GetOrCreateConnection(context, out 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 // Mark the connection as active
state.Active = true; state.Active = true;