WebSocket doesn't work in MicroSoft Edge

李畋岷 76 Reputation points
2021-12-07T07:40:31.073+00:00

My relevant js code is as follows:

155549-tw2prt1xfopn4uaao3bcf.png

But it doesn't work in Edge.The "socket.onmessage" was not entered at all.
My Edge's version is 96.0.1054.43.To solve this problem, I have done the following work:

  1. The following settings are made on the Edge's about:flags page:
    6@$_C(11~TX~@` 1JZ%9 EH
  2. Run the command Get-AppxPackage in PowerShell to get the Edge's PackageFamilyName,then use CheckNetIsolation LoopbackExempt -a -n= PackageFamilyName in cmd.
    3,The following settings are made in the registry:
    IU1BATG99 QS$R%MK AV}O4

But none of the above works. What's going on? What should I do?

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,346 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,596 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,593 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,741 Reputation points Microsoft Vendor
    2021-12-08T06:00:24.16+00:00

    Hi @李畋岷 ,

    Based on the third line of the code you provided, it should get something like this: wss://<hostname>:<port>/wss. Is that what you expected?

    On the other hand, I'm not sure if you created a websocket server for it, I created websocket server using NodeJS and did a simple test, and I found that it works correctly in Edge version 96.0.1054.43. If possible, could you provide more details, such as if there are any error messages in the console or provide complete code example to reproduce this issue?

    Here is my test below:

    const WebSocket = require('ws');  
    const WebSocketServer = WebSocket.Server;  
    const wss = new WebSocketServer({  
        port: 8080  
    });  
    

    ----------

    155823-image.png

    And result below:
    riICb.png

    You could also refer to this doc for more details about websocket server.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. 李畋岷 76 Reputation points
    2021-12-11T02:16:22.94+00:00

    @XuDong Peng-MSFT @AgaveJoe
    The problem has been solved. The reason is that the js statement "clipboardData" is not supported in Edge. The exception caused by this statement causes the subsequent process to fail to execute properly. So the websocket can't work.
    Is there a substitute for clipboardData in Edge? In addition, I put the tags < table ><tbody></tbody></table> in a Razor component. The component's render-mode must be set to WebAssemblyPrerendered to display images properly. But in this way, the click event of another component cannot be responded to. It seems that this WebAssemblyPrerendered also has an impact on other components. Is there any way to make the setting of render-mode affect only the component in which it is located?


  3. 李畋岷 76 Reputation points
    2021-12-08T07:12:52.25+00:00

    This is a asp.net core web project developed in vs2019. Websocket can work well in ie. I add app.UseMiddleware<WsHandleMiddleware>(_options) to Configure method in Startup.cs. So,the Websocket server should exist.

    WsHandleMiddleware.cs:

    using ElecArcSys.Entities;
    using Microsoft.AspNetCore.Http;
    using Microsoft.EntityFrameworkCore;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net.WebSockets;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    namespace ElecArcSys.Infrastructure
    {
    public class WsHandleMiddleware
    {
    private RequestDelegate nextDelegate;
    private readonly DbContextOptions<BIFUDbContext> _options;
    public WsHandleMiddleware(RequestDelegate next, DbContextOptions<BIFUDbContext> options)
    {
    nextDelegate = next;
    _options=options;
    }
    public async Task Invoke(HttpContext context)
    {
    if (context.Request.Path == "/wss")
    {
    if (context.WebSockets.IsWebSocketRequest)
    {
    using (WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync())
    {
    await Echo(context, webSocket);
    }
    }
    else
    {
    context.Response.StatusCode = 400;
    }
    }
    else
    {
    await nextDelegate.Invoke(context);
    }
    }
    private async Task Echo(HttpContext context, WebSocket webSocket)
    {
    var buffer = new byte[1024 * 4];
    WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
    var list = (IList<byte>)buffer;
    byte[] bt = new byte[4096];
    int i = 0;
    while (list[i] != 0)
    {
    bt[i] = list[i];
    i++;
    }
    string message = Encoding.UTF8.GetString(bt);
    StringReader reader = new StringReader(message);
    var name = reader.ReadLine().Trim();
    var PasWd = reader.ReadLine().Trim();
    PasWd=new string((from c in PasWd.ToCharArray() where !char.IsControl(c) select c).ToArray());
    while (!result.CloseStatus.HasValue)
    {
    CaseImg CRd = new CaseImg();
    var ct = new BIFUDbContext(_options);
    CRd = ct.CaseImgs.ToList().ElementAt(0);
    Image image = Image.FromFile(".\ElecImg\" + CRd.Imgpath);
    MemoryStream ms = new MemoryStream();
    image.Save(ms, image.RawFormat);
    byte[] byteArray = ms.ToArray();
    string ImgStr = Convert.ToBase64String(byteArray);
    await webSocket.SendAsync(Encoding.UTF8.GetBytes(ImgStr), WebSocketMessageType.Text, true, CancellationToken.None);
    result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
    }
    await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
    }
    }
    }

    The codes above work well in ie.

    Best regards

    0 comments No comments

  4. AgaveJoe 28,456 Reputation points
    2021-12-10T14:38:21.967+00:00

    Maybe I do not understand but it seems like you are creating a Blazor Server application and adding a web socket connection. If so, Blazor Server uses a web socket (SignalR) listening on the current domain and port. Adding another web socket middleware listener on the same domain and port is not going to work.

    Can you clarify the design intention?

    Also, I recommend testing your socket server and code in a basic HTML page and a .NET 5/6 project other than Blazor Server. You can find web socket samples on GitHub.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.