简体   繁体   English

格式化用户使用Javascript发送的聊天消息

[英]Formatting chat messages sent by user in Javascript

I've created a websocket server in python and I have a web chat which uses that web server, the web chat box(which displays the messages sent by the user) has the height of 83px and width of 1176px. 我用python创建了一个网络套接字服务器,并且有一个使用该网络服务器的网络聊天,该网络聊天框(显示用户发送的消息)的高度为83px,宽度为1176px。

I'd like for my users messages to be displayed horizontally along the chat box(as accordingly with the chat box size specifications) instead of the conventional list type display of messages, so it seems as if my users are completing each other's sentences. 我希望用户的消息沿聊天框水平显示(相应地取决于聊天框大小规格),而不是常规的列表类型的消息显示,因此好像我的用户似乎正在完成彼此的句子。

Further clarifying what I mean: 进一步阐明我的意思:

userA types: "Hi my name is James" userA类型:“嗨,我叫詹姆斯”

userB types: "Of course it is" userB类型:“当然是”

Chatbox output: Hi my name is James Of course it is 聊天框输出:嗨,我叫詹姆斯,当然是

Is there any effective method of achieving this task? 有没有有效的方法来完成这项任务? Here's what I have so far, be warned, it's not much! 到目前为止,这是我所收到的警告,数量不多!

s = new WebSocket(host);

                    s.onopen = function (e) { log_msg("connected..."); };
                    s.onclose = function (e) { log_msg("connection closed."); };
                    s.onerror = function (e) { log_msg("connection error."); };
                    s.onmessage = function (e) { log_msg("message: " + e.data); };
                } catch (ex) {

HTML HTML

<fieldset id="messages" class = "focus-actions""> </fieldset>

More Code as requested 根据要求提供更多代码

var messages;
            var form;
            var inputBox;

            function log_msg(msg) {
                var p = document.createElement("p");
                p.innerHTML = msg;
                messages.appendChild(p);
            }

            function doInit() {
                inputBox = document.getElementById("message");
                messages = document.getElementById("messages");
                form = document.getElementById("message-form");
                var s;
                try {
                    var host = "ws://localhost:4545/";
                    if(window.location.hostname) {
                        host = "ws://" + window.location.hostname + ":4545/";
                    }

                    s = new WebSocket(host);

                    s.onopen = function (e) { log_msg("connected..."); };
                    s.onclose = function (e) { log_msg("connection closed."); };
                    s.onerror = function (e) { log_msg("connection error."); };
                    s.onmessage = function (e) { log_msg("message: " + e.data); };
                } catch (ex) {
                    log_msg("connection exception:" + ex);
                }

                form.addEventListener("submit", function (e) {
                    e.preventDefault();
                    s.send(inputBox.value);
                    inputBox.value = "";
                }, false);
            }

In your current function log_msg : 在您当前的函数log_msg

function log_msg(msg) {
    var p = document.createElement("p");
    p.innerHTML = msg;
    messages.appendChild(p);
}

you are creating 'paragraph' elements which are block-level elements (as opposed to inline elements). 您正在创建“段落”元素,它们是块级元素(与内联元素相对)。

You'd either create alternate styling rules for p elements in css: 您可以为css中的p元素创建替代的样式规则:

#messages p    { display: inline }

for those paragraphs (you might need to specify/override other browser-specific default styles to), 对于这些段落(您可能需要指定/替代其他特定于浏览器的默认样式),

OR (more logically) you change it to create a span-element (which is inline): 或(从逻辑上讲),您可以对其进行更改以创建一个跨度元素(内联):

In your function log_msg : 在您的函数log_msg

function log_msg(msg) {
    var p = document.createElement("span");
    p.innerHTML = msg;
    messages.appendChild(p);
}

or simplified: 或简化:

function log_msg(msg) {
    messages.appendChild(document.createElement("span")).innerHTML = msg;
}  //appendChild returns child, thats why you can set innerHTML directly

Note that you might want to take leading and trailing whitespace (especially the lack of) into account for your msg ! 请注意,您可能要考虑msg前导和尾随空格(尤其是缺少空格)! Example: 例:

msg.trim()+' '   // or for older browsers: msg.replace(/^\s+|\s+$/g, '')+' '

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM