sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.71k stars 1.66k forks source link

Browser displays html as plain text in Unity #64

Open mruce opened 10 years ago

mruce commented 10 years ago

Hi, I'm using websocket-sharp in my unity project, it works great in editor. However there are issues with http server after deployment - it sends requested file, but browser displays it as plain text - with header on top:

P/1.1 200 OK Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Expires: 0 Server: websocket-sharp/1.0 Date: Tue, 05 Aug 2014 14:28:15 GMT Content-Length: 4484 Keep-Alive: timeout=15,max=100

<!DOCTYPE html>

... App is running on exact same machine, only difference is running in editor or a standalone build. Running on Windows 7, with Polish lang.
sta commented 10 years ago

Hello there,

Does that server execute from your script in Unity?

And could you show me your server code?

mruce commented 10 years ago

Yes, server is inside Unity app. I simplified the code and pasted it below. Complete project (including compiled app) is available here: https://dl.dropboxusercontent.com/u/2091806/_Temp/Server.rar

App is expecting index.html to be placed in d:\

using UnityEngine;
using System.Collections;
using WebSocketSharp.Server;
using System;
using WebSocketSharp;
using WebSocketSharp.Net;

public class MrServer : MonoBehaviour
{    
    public int _PortNr = 9999;
    public string _RootPath = @"";    

    private HttpServer _socketServer;

    public virtual void Start()
    {
        _socketServer = new HttpServer(_PortNr);

        _socketServer.RootPath = _RootPath;
        Debug.Log("Root path: " + _socketServer.RootPath);
        Debug.Log("Port nr: " + _socketServer.Port);
        _socketServer.OnGet += (sender, e) => onGet(e);

        _socketServer.Log.Level = LogLevel.TRACE;
        _socketServer.Log.SetOutput((l, s) => { Debug.Log(l.Message); });

        _socketServer.AddWebSocketService<Speedway>("/Speedway");

        try
        {
            _socketServer.Start();
        }
        catch (Exception e) { Debug.Log(e); }        
    }

    private byte[] getContent(string path)
    {
        if (path == "/")
            path += "index.html";
        Debug.Log("Getting " + path);
        return _socketServer.GetFile(path);
    }

    private void onGet(HttpRequestEventArgs e)
    {        
        var time = DateTime.Now;
        var req = e.Request;
        var res = e.Response;

        byte[] content = getContent(req.RawUrl);
        if (content != null)
        {
            res.WriteContent(content);
            Debug.Log(string.Format("Sent {0} in {1} mS", e.Request.RawUrl, (int)(DateTime.Now - time).TotalMilliseconds));
            return;
        }

        res.StatusCode = (int)HttpStatusCode.NotFound;
    }

    public virtual void OnDestroy()
    {
        Debug.Log("Destroying!");
        _socketServer.Stop();
    }

}

public class Speedway : WebSocketService
{
    protected override void OnOpen()
    {
        base.OnOpen();
    }
}
gtk2k commented 10 years ago

I have similar problem on index.html file encoding "UTF-8(BOM)". File encoding change to "UTF-8N(non BOM)" problem clear in my case.

mruce commented 10 years ago

Hi gtk2k, could you specify your solution a bit more precise? Tried to change encoding (in Notepad++), but didn't work as expected.

mruce commented 10 years ago

A little bump.