Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:35+00:00 2026-05-26T12:35:35+00:00

I wish to have an app written in the D programming language update its

  • 0

I wish to have an app written in the D programming language update its display in a browser. The browser should also send input data back to the app.

I’m still quite new to programming and am confused with how sockets/websockets/servers all fit together. Can anyone suggest an approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:35:36+00:00Added an answer on May 26, 2026 at 12:35 pm

    Many thanks to gmfawcett for the link to his basic D server example which I’ve mated with a bare-bones websocket implementation of the version 8 spec that I found elsewhere (currently only works in Chrome 14/15, I believe). It’s pretty much cut’n’paste but seems to work well enough and I expect it will be sufficient in serving my needs.

    If anyone has the inclination to cast a quick eye over my code for any glaring no-nos, please feel free to do so – and thanks!

    Bare-bones websocket impl: http://blog.vunie.com/implementing-websocket-draft-10

    Websocket v8 spec (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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app where users select images they wish to print, the print
I have a Sinatra app which would be used by different clients. I wish
I have a SIP app that I wish to connect to a Lync environment.
I have an app with 2 tabbed view, I wish to execute some code
I need to do this: I have an app with a button, I wish,
I have several applications in App Store and I wish to get further advanced
I have created save.plist in a resource folder. I have written some data within
I am writing a custom xulrunner-based app and I wish to have some files
I have a c# app that I am working on and wish to run
I have written for a very simple app of mine . To login facebook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.