当前环境不支持使用socket.io的WebSockets时,有什么好处?
我正在制作一个可以让多个玩家连接的应用程序,但我正在集思广益。
我将使用电子作为界面,然后能够使用其他计算机登录网站以与多个玩家一起加入游戏。
我将使用套接字连接到每个播放器,但是我想弄清楚的是接口的最佳方法。我应该使用带电子快车以某种方式连接到玩家所在的网站吗?
是否可以使电子应用程序启动服务器并将我的网站直接定向到运行电子应用程序的计算机?
或者,我的另一种选择是只是连续地从数据库上载和下载,但是我觉得那不是很有效。
假设我正在创建一个websocket客户端.并且特定的websocket url将框架返回为"二进制框架(操作码2)".问题是
1.为什么开发人员想要将原始消息包装在二进制操作码框架内?
2.检索以实现为中心的消息吗?换句话说,客户端是否必须知道用于在服务器上编码的相同逻辑?
3.如果上述内容为false,那么是否存在解码/解析二进制操作码以查看正在发送的实际数据的全局方法?
ASP.NET Core应用程序在客户端使用websocket连接,在服务器端使用Microsoft.AspNetCore.WebSockets.Server 0.1.0(nuget上的最新稳定版本).简单的发送代码是
await _ws.SendAsync(new ArraySegment<byte>(arrbr), WebSocketMessageType.Text, true, ctk);
Run Code Online (Sandbox Code Playgroud)
问题是这条线在关闭连接时抛出错误.如果进程成功,我希望该方法返回一个布尔值.我已经检查连接是否打开如下:
_ws.State == WebSocketState.Open
Run Code Online (Sandbox Code Playgroud)
但是,如果用户已拔下网络电缆或断开其设备(几乎所有情况除了关闭浏览器),这都不起作用.
作为额外的,我不知道如何模拟两个客户端之一的网络连接丢失,我认为WebSocketState是只读的,请警告我,如果我错了,我不认为更短的ping会解决这个问题.
我有两个想法:
我可以在try catch块中使用发送者代码.(我不习惯在生产代码中使用try catch)
我可以在客户端设置间隔,并向服务器询问"对我来说什么是新的".我觉得这很糟糕,因为它远不是一个websocket(更接近http).
有没有办法解决这个问题而不使用try catch?我知道这不是一个合格的问题,而是一个合格的问题.如果需要,我可以分享完整的代码.
使用服务器端日志记录后:
虽然消息在生产环境中运行良好,但我通过拔下网络电缆并将数据发送到客户端来断开客户端连接.我使用try catch而没有捕获.然后我得到这个错误.
这意味着我无法通过使用try catch来检测丢失的连接.我想我可以通过处理这个投掷来解决这个问题.
我该如何处理这个错误?
我注意到"异步空虚方法的例外不能被Catch捕获"和"它可以在catch中使用await",因为c#6 链接但是我无法捕获.如果我不能以这种方式修复,
我可能会尝试同步运行await _ws.SendAsync(new ArraySegment<byte>(arrbr), WebSocketMessageType.Text, true, ctk).runsynchronously();
同步运行没有帮助.使用try catch没有区别.结果提问,asp.net核心websocket如何ping
使用Microsoft.aspnetcore.websockets.server 0.1.0时的日志文件
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HL1940BSEV8O": An unhandled exception was thrown by the application.
System.IO.IOException: Unexpected end of stream
at Microsoft.AspNetCore.WebSockets.Protocol.CommonWebSocket.<EnsureDataAvailableOrReadAsync>d__38.MoveNext()
使用Microsoft.aspnetcore.websockets 1.0.0时的日志文件
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HL19H5R4CS21": An unhandled …
这是关于WS和的简单聊天express.js.我收到浏览器无法通过连接到服务器的错误websockets.
客户连接:
file: rtc.html
ws = new WebSocket('wss://' + window.location.hostname + '/wr' );
ws.onerror = (error) => { console.log(error); };
ws.onmessage = (message) => {
. . .
Run Code Online (Sandbox Code Playgroud)
服务器代码:
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
app.get('/rtc', (req, res)=>{
res.sendFile('/home/user/dev/rtc.html');
});
const server = http.createServer(app);
const wss = new WebSocket.Server({ server:server, path: "/wr" });
. . .
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
UPD:问题是由于我正在进行聊天webrtc和测试,Mozilla并且Mozilla没有连接就无法 …
对我来说第一要点:我想实现websockets.我不需要socketIO的后备选项.
我希望"我的客户"能够实现他们想要的任何东西,只要他们遵守websockets协议.即类似于:var ws = new WebSocket.
那么..如果服务器是Flask-SocketIO,一个简单的js WebSocket会起作用吗?
ADDIONAL NOTES:Python首先!我正在尝试设置一个服务器,它只响应(实际上只发送)到websockets,没有关联的网页.(是的,我对WS很好,我不需要WSS,以防你问;)).我试着在服务器端使用烧瓶插座 https://github.com/kennethreitz/flask-sockets, 但它给了我一些问题.就像立即关闭连接和网络上的许多类似问题一样,我找不到解决方案.很难调试.所以在我开始开发新服务器之前......
我想通过Web套接字向特定用户发送消息。到目前为止,我可以打开Web套接字并像这样从客户端读取消息:
@ServerEndpoint(value = "/wsep")
public class WebSocketEndpoint {
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketEndpoint.class);
private Session session;
@OnOpen
public void onOpen(Session session) {
this.session = session;
try {
session.getBasicRemote().sendText("You are connected. Your ID is " + session.getId());
} catch (Exception e) {
LOGGER.error("Error on open web socket", e);
}
}
@OnMessage
public void onClientMessage(String message, Session session) {
LOGGER.info("Message from {} is: {}", session.getId(), message);
}
@OnClose
public void onClose(Session session) {
this.session = null;
LOGGER.info("{} disconnected", session.getId());
} …Run Code Online (Sandbox Code Playgroud) 如何从Java Vertex连接Cex.IO Websocket API?
问题是Vert.x不能像Node.JS那样为我提供仅与WsURI连接的方法。我必须指定端口和主机并获取HTTP 400 Bad Request异常。
使用Node.js,您可以:
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.connect("wss://ws.cex.io/ws/");
Run Code Online (Sandbox Code Playgroud)
使用Vert.x,您必须做
int host = 443; // That's defaults
String host = "cex.io"; // Am I right by specifying this host?
HttpClient client = Vertx.vertx().createHttpClient();
client.websocket(port, host, "wss://ws.cex.io/ws/", ws -> { ...});
Run Code Online (Sandbox Code Playgroud) 我使用Spring Boot和WebSocket项目,当我试图保持(保存)输出消息时,我收到错误.我打印了要提供的实体,
Inout MessageMessage{id=null, from='Mark', text='I travel to Brazil'}
Output MessageOutputMessage{id=null, from='Mark', message='I travel to Brazil', topic='Travel', time=Tue Nov 14 10:53:06 BDT 2017}
Hibernate: insert into output_message (from, message, time, topic) values (?, ?, ?, ?)
Run Code Online (Sandbox Code Playgroud)
错误消息如下所示,
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] …Run Code Online (Sandbox Code Playgroud) 我一直试图使用Pusher PHP Server触发事件,但无法这样做.我已经尝试了几种解决方案,例如在我的服务器环境中启用cURL扩展但仍然没有结果.我已经成功地使用Pusher Debug Console提供的事件创建器触发事件.我错过了什么吗?
客户端代码片段(订阅频道和收听活动)
<head>
<title>Pusher Test</title>
<script src="//js.pusher.com/2.2/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">
Pusher.log = function(message) {
if (window.console && window.console.log) {
window.console.log(message);
}
};
var pusher = new Pusher('key');
var channel = pusher.subscribe('test_channel');
channel.bind('my_event', function(data) {
alert(data.message);
});
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
服务器端代码片段(不工作)
require('Pusher.php');
$app_id = 'xxx';
$app_key = 'xxx';
$app_secret = 'xxx';
$pusher = new Pusher($app_key, $app_secret, $app_id);
$data['message'] = 'hello world';
$pusher->trigger('test_channel', 'my_event', $data);
Run Code Online (Sandbox Code Playgroud)