gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:53:19 25 4
gpt4 key购买 nike

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

我对编程还是很陌生,并且对套接字/websockets/服务器如何组合在一起感到困惑。任何人都可以提出一种方法吗?

最佳答案

非常感谢 gmfawcett 提供了指向他的基本 D 服务器示例的链接,我已经与我在其他地方找到的版本 8 规范的准系统 websocket 实现配合使用(我相信目前仅适用于 Chrome 14/15)。它几乎是剪切'n'paste,但似乎工作得很好,我希望它足以满足我的需求。
如果有人愿意快速浏览我的代码以发现任何明显的禁忌,请随时这样做 - 谢谢!
准系统 websocket 实现:http://blog.vunie.com/implementing-websocket-draft-10
Websocket v8 规范(协议(protocol) 17):https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-17

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);
}
}

关于sockets - 允许 D 应用程序和浏览器之间双向通信的工具链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com