Skip to content

Commit 8e01678

Browse files
committed
Example - Update Home.html
Fix some mistakes
1 parent 572a05b commit 8e01678

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

CefSharp.Example/Resources/Home.html

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -234,52 +234,43 @@ <h3 id="features-custom-schemes">Custom Schemes</h3>
234234
...and the actual scheme handler class like this:
235235

236236
<pre data-shbrush="csharp">
237-
internal class CefSharpSchemeHandler : IResourceHandler
237+
//This Resource handler (scheme handler factory returns IResourceHandler) downloads a file
238+
// and servces the content as the response, you could just as easily read from a database or disk
239+
public class FlashResourceHandler : ResourceHandler
238240
{
239-
private string mimeType;
240-
private MemoryStream stream;
241-
242-
public bool ProcessRequestAsync(IRequest request, ICallback callback)
241+
public override bool ProcessRequestAsync(IRequest request, ICallback callback)
243242
{
244-
// NOTE: We suggest you structure your code in an async fashion
245-
// First examine the "request" object for info about the URI being requested and so forth.
246-
// If the Url is valid then spawn a task
247-
248243
Task.Run(() =>
249244
{
250-
// In this task you can perform your time consuming operations, e.g query a database
251-
// NOTE: We suggest that you wrap callbacks in a using statemnt so that they're disposed
252-
// even if there is an exception as they wrap an unmanaged response which will cause memory
253-
// leaks if not freed
254245
using (callback)
255246
{
256-
// Read the data in, set the mime type
257-
var bytes = Encoding.UTF8.GetBytes(resource);
258-
stream = new MemoryStream(bytes);
259-
var fileExtension = Path.GetExtension(fileName);
260-
mimeType = ResourceHandler.GetMimeType(fileExtension);
261-
262-
// When your finished processing execute the callback.
263-
// Most callbacks have multiple methods, so checkout their interface for details
247+
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://samples.mplayerhq.hu/SWF/zeldaADPCM5bit.swf");
248+
249+
var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
250+
251+
// Get the stream associated with the response.
252+
var receiveStream = httpWebResponse.GetResponseStream();
253+
var mime = httpWebResponse.ContentType;
254+
255+
var stream = new MemoryStream();
256+
receiveStream.CopyTo(stream);
257+
httpWebResponse.Close();
258+
259+
//Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer
260+
stream.Position = 0;
261+
262+
//Populate the response values - No longer need to implement GetResponseHeaders (unless you need to perform a redirect)
263+
ResponseLength = stream.Length;
264+
MimeType = mime;
265+
StatusCode = (int)HttpStatusCode.OK;
266+
Stream = stream;
267+
264268
callback.Continue();
265269
}
266270
});
267271

268-
// Return true to indicate that you've handled the request, you can return false which will cancel the request
269272
return true;
270273
}
271-
272-
public Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl)
273-
{
274-
// NOTE: GetResponse will be executed after you have called Continue()
275-
// You simple need to populate the relevant details, note you can also redirec to another url if required
276-
responseLength = stream.Length;
277-
redirectUrl = null;
278-
response.StatusCode = (int)HttpStatusCode.OK;
279-
response.StatusText = "OK";
280-
response.MimeType = mimeType;
281-
return stream;
282-
}
283274
}
284275
</pre>
285276
Finally, you have to register this scheme handler using some code like this:
@@ -319,11 +310,13 @@ <h3 id="features-javascript-integration">JavaScript integration</h3>
319310
of the block below will be modified/inspected by the script code.
320311

321312
<pre id="modify-me">You can modify the value of this text field using JavaScript!</pre>
313+
For more details and further examples read <a href="https://github.com/cefsharp/CefSharp/wiki/General-Usage#javascript-integration">General Usage Guide - Javascript Integration</a>
322314

323315
The C# code for performing these kinds of interactions is quite simple. Like this:
324316
</p>
325317

326318
<pre data-shbrush="csharp">
319+
using CefSharp; //EvaluateScriptAsync is an extension method
327320
webBrowser.ExecuteScriptAsync(someScriptCode);
328321
</pre>
329322

@@ -342,15 +335,15 @@ <h3 id="features-javascript-integration">JavaScript integration</h3>
342335
</p>
343336

344337
<pre data-shbrush="csharp">
345-
var result = webBrowser.EvaluateScript("10 + 20");
338+
using CefSharp; //EvaluateScriptAsync is an extension method
339+
var result = await webBrowser.EvaluateScriptAsync("10 + 20");
346340
</pre>
347341

348342
<p>
349343
Please note that only a limited number of data types are supported when returning the result above. Simple
350344
value types (int, float, etc) and strings all work, but do not expect to be able to return other JavaScript
351345
objects.
352346
</p>
353-
<h3>Hooks by which you can modify and/or override certain features of the web browsing</h3>
354347
</div>
355348
<div class="bs-docs-section">
356349
<div class="page-header">

0 commit comments

Comments
 (0)