Sockets 允许D应用程序和浏览器之间双向通信的工具链

Sockets 允许D应用程序和浏览器之间双向通信的工具链,sockets,websocket,d,Sockets,Websocket,D,我希望有一个应用程序写在D编程语言更新其显示在浏览器中。浏览器还应将输入数据发送回应用程序 我还是一个编程新手,对socket/websockets/server是如何组合在一起感到困惑。有人能推荐一种方法吗?非常感谢gmfawcett提供了他基本D服务器示例的链接,我已经将其与我在别处找到的版本8规范的基本websocket实现进行了匹配(我相信目前仅适用于Chrome 14/15)。这几乎是剪成的糊,但似乎效果很好,我希望它能满足我的需要 如果有人想快速浏览一下我的代码,看看有没有什么明显的

我希望有一个应用程序写在D编程语言更新其显示在浏览器中。浏览器还应将输入数据发送回应用程序


我还是一个编程新手,对socket/websockets/server是如何组合在一起感到困惑。有人能推荐一种方法吗?

非常感谢gmfawcett提供了他基本D服务器示例的链接,我已经将其与我在别处找到的版本8规范的基本websocket实现进行了匹配(我相信目前仅适用于Chrome 14/15)。这几乎是剪成的糊,但似乎效果很好,我希望它能满足我的需要

如果有人想快速浏览一下我的代码,看看有没有什么明显的“不”的地方,请随便看看——谢谢

裸骨骼websocket impl:

Websocket v8规范(协议17):

模块wsserver;
导入标准算法;
进口标准base64;
进口标准conv;
进口std.stdio;
进口标准插座;
输入标准字符串;
//标准加密:https://github.com/pszturmaj/phobos/tree/master/std/crypto
导入crypto.hash.base;
导入crypto.hash.sha;
结构WsServer
{
私有的
{
插座;
插座连接;
字符串子程序;
}
这(字符串主机,ushort端口=8080,字符串subtocol=“null”)
{
this.subtocol=subtocol;
s=新的TcpSocket(AddressFamily.INET);
s、 绑定(新的InternetAddress(主机、端口));
s、 听(8);
conn=s.accept();
writeln(“将浏览器指向/刷新到\“http://”,主机“\”以启动websocket握手”);
尝试
{
初始化握手(conn);
}
捕获(可丢弃的e)
{
stderr.writeln(“抛出:”,e);
}
}
~z~这个()
{
连接关闭(SocketShutdown.BOTH);
康涅狄格州关闭();
s、 关机(SocketShutdown.BOTH);
s、 close();
}
字符串数据()
{
ubyte[8192]msgBuf;
自动msgBufLen=conn.receive(msgBuf);
auto firstByte=msgBuf[0];
自动秒字节=msgBuf[1];
//不确定这两项检查是否正确!!!
强制((firstByte&0x81),“不支持片段”);//存在强制FIN位
强制((secondByte&0x80),“屏蔽位不存在”);//强制屏蔽位存在
自动msgLen=secondByte&0x7f;
ubyte[]面膜,味精;
如果(msgLen<126)
{
mask=msgBuf[2..6];
msg=msgBuf[6..msgBufLen];
}
否则如果(msgLen==126)
{
mask=msgBuf[4..8];
msg=msgBuf[8..msgBufLen];
}
foreach(i,参考e;msg)
e=msg[i]^mask[i%4];
调试writeln(“客户端:”~cast(string)msg);
返回cast(string)消息;
}
无效数据(字符串消息)
{
ubyte[]新框架;
如果(消息长度>125)
newFrame=newubyte[4];
其他的
newFrame=newubyte[2];
新帧[0]=0x81;
如果(消息长度>125)
{
新帧[1]=126;
newFrame[2]=cast(ubyte)msg.length>>8;
新帧[3]=msg.length&0xFF;
}
其他的
newFrame[1]=强制转换(ubyte)msg.length;
conn.send(newFrame~=msg);
调试writeln(“服务器:”~msg);
}
专用握手(套接字连接)
{
ubyte[8192]buf;//足够大,可以用于某些用途。。。
尺寸位置、头部、透镜、新位置;
//在解析前接收整个标头。
while(true)
{
len=conn.receive(buf[位置..$]);
调试writeln(转换(字符串)buf);
if(len==0)//空请求
返回;
新位置=位置+透镜;
headerEnd=countUntil(buf[position..newpos],“\r\n\r\n”);
位置=新位置;
如果(headerEnd>=0)
打破
}
//现在分析标题。
自动行=拆分器(buf[0..headerEnd],“\r\n”);
string request_line=cast(string)line.front;
前线;
//一个非常简单的头结构。
结构对
{
字符串键,值;
此(ubyte[]行)
{
自动tmp=countUntil(行“:”);
key=cast(string)行[0..tmp];//可能是这种情况吗?
值=转换(字符串)行[tmp+2..$];
}
}
对[]头;
foreach(行;行)
标题~=对(行);
自动tmp=拆分器(请求行“”);
字符串方法=tmp.front;tmp.popFront;
字符串url=tmp.front;tmp.popFront;
字符串协议=tmp.front;tmp.popFront;
枚举GUID_v8=“258EAFA5-E914-47DA-95CA-C5AB0DC85B11”//版本8规范…可能会更改
自动sha1=新sha1;
sha1.put(strip(headers[5].value)~GUID\u v8);
auto respKey=to!string(Base64.encode(sha1.finish());
//准备一个响应并发送它
string resp=join([“HTTP/1.1 101交换协议”,
“升级:websocket”,
“连接:升级”,
“Sec WebSocket接受:”~respKey,
“Sec WebSocket协议:”~subtocol,
""],
“\r\n”);
conn.send(cast(ubyte[])(resp~“\r\n”);
调试写入(resp);
}
}

听起来你在问,“如何用D编写Web应用程序?”你提到对套接字、服务器等感到困惑。。。如果您试图编写D Web应用程序,这可能是一个进入的障碍。有一些有趣的第三方(非标准库)工具用于编写D Web应用程序,但我认为它们假定您熟悉Web服务器的工作方式,并且
module wsserver;

import std.algorithm;
import std.base64;
import std.conv;
import std.stdio;
import std.socket;
import std.string;

//std.crypto: https://github.com/pszturmaj/phobos/tree/master/std/crypto
import crypto.hash.base;
import crypto.hash.sha;

struct WsServer
{
    private
    {
        Socket s;
        Socket conn;
        string subProtocol;
    }

    this(string host, ushort port = 8080, string subProtocol = "null")
    {
        this.subProtocol = subProtocol;

        s = new TcpSocket(AddressFamily.INET);
        s.bind(new InternetAddress(host, port));
        s.listen(8);

        conn = s.accept();

        writeln("point/refresh your browser to \"http://", host, "\" to intiate the websocket handshake");

        try
        {
           initHandshake(conn);
        }
        catch (Throwable e)
        {
            stderr.writeln("thrown: ", e);
        }
    }

    ~this()
    {
        conn.shutdown(SocketShutdown.BOTH);
        conn.close();

        s.shutdown(SocketShutdown.BOTH);
        s.close();
    }

    string data()
    {
        ubyte[8192] msgBuf;
        auto msgBufLen = conn.receive(msgBuf);

        auto firstByte = msgBuf[0];
        auto secondByte = msgBuf[1];

        // not sure these two checks are woking correctly!!!
        enforce((firstByte & 0x81), "Fragments not supported"); // enforce FIN bit is present
        enforce((secondByte & 0x80), "Masking bit not present"); // enforce masking bit is present

        auto msgLen = secondByte & 0x7f;

        ubyte[] mask, msg;

        if(msgLen < 126)
        {
            mask = msgBuf[2..6];
            msg = msgBuf[6..msgBufLen];
        }
        else if (msgLen == 126)
        {
            mask = msgBuf[4..8];
            msg = msgBuf[8..msgBufLen];
        }

        foreach (i, ref e; msg)
            e = msg[i] ^ mask[i%4];

        debug writeln("Client: " ~ cast(string) msg);

        return cast(string) msg;
    }

    void data(string msg)
    {
        ubyte[] newFrame;

        if (msg.length > 125)
            newFrame = new ubyte[4];
        else
            newFrame = new ubyte[2];

        newFrame[0] = 0x81;

        if (msg.length > 125)
        {
            newFrame[1] = 126;

            newFrame[2] = cast(ubyte) msg.length >> 8;
            newFrame[3] = msg.length & 0xFF;
        }
        else
            newFrame[1] = cast(ubyte) msg.length;

        conn.send(newFrame ~= msg);

        debug writeln("Server: " ~ msg);
    }

    private void initHandshake(Socket conn)
    {
        ubyte[8192] buf;  // big enough for some purposes...
        size_t position, headerEnd, len, newpos;

        // Receive the whole header before parsing it.
        while (true)
        {
            len = conn.receive(buf[position..$]);

            debug writeln(cast(string)buf);

            if (len == 0)               // empty request
              return;

            newpos = position + len;
            headerEnd = countUntil(buf[position..newpos], "\r\n\r\n");
            position = newpos;

            if (headerEnd >= 0)
                break;
        }

        // Now parse the header.
        auto lines = splitter(buf[0..headerEnd], "\r\n");
        string request_line = cast(string) lines.front;
        lines.popFront;

        // a very simple Header structure.
        struct Pair
        {
            string key, value;

            this(ubyte[] line)
            {
              auto tmp = countUntil(line, ": ");
              key = cast(string) line[0..tmp]; // maybe down-case these?
              value = cast(string) line[tmp+2..$];
            }
        }

        Pair[] headers;
        foreach(line; lines)
            headers ~= Pair(line);

        auto tmp            = splitter(request_line, ' ');
        string method       = tmp.front; tmp.popFront;
        string url          = tmp.front; tmp.popFront;
        string protocol     = tmp.front; tmp.popFront;

        enum GUID_v8 = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; // version 8 spec... might change
        auto sha1 = new SHA1;
        sha1.put(strip(headers[5].value) ~ GUID_v8);
        auto respKey = to!string(Base64.encode(sha1.finish()));

        // Prepare a response, and send it
        string resp = join(["HTTP/1.1 101 Switching Protocols",
                            "Upgrade: websocket",
                            "Connection: Upgrade",
                            "Sec-WebSocket-Accept: " ~ respKey,
                            "Sec-WebSocket-Protocol: " ~ subProtocol,
                            ""],
                            "\r\n");

        conn.send(cast(ubyte[]) (resp ~ "\r\n"));

        debug writeln(resp);
    }
}