首页 \ 问答 \ SparkJava使用Kotlin和WebSockets(SparkJava using Kotlin and WebSockets)

SparkJava使用Kotlin和WebSockets(SparkJava using Kotlin and WebSockets)

我正在使用SparkJava尝试Kotlin,并且无法实现WebSockets路由。 我试图遵循SparkJava网站( http://sparkjava.com/tutorials/websocket-chat )上提供的WebSockets示例,虽然我可以使OnWebSocketConnect和OnWebSocketMessage元素工作,但是没有拾取OnWebSocketClose。

我已经用Java实现了这个,以便仔细检查它是不是浏览器问题,并且Java实现工作得很好......所以这似乎是Kotlin解释OnWebSocketClose注释的方式所特有的。

我的代码如下所示

import spark.Spark.*
import org.eclipse.jetty.websocket.api.Session
import org.eclipse.jetty.websocket.api.annotations.*;

fun main(args: Array<String>) {
    staticFileLocation("/public")
    webSocket("/chat", WSHandler::class.java)
    init()
}

@WebSocket
class WSHandler {

    @OnWebSocketConnect
    fun connected(session: Session) = println("session connected")

    @OnWebSocketClose
    fun closed(session: Session, statusCode: Int, reason: String) = println("closed sessions")

    @OnWebSocketMessage
    fun message(session: Session, message: String) = println("Got: $message")
}

html / javascript等符合SparkJava网站上的教程。


I am trying out Kotlin with SparkJava, and having trouble implementing the WebSockets routes. I am trying to follow the WebSockets example available on the SparkJava website (http://sparkjava.com/tutorials/websocket-chat), and whilst I can get the OnWebSocketConnect and OnWebSocketMessage elements to work, the OnWebSocketClose is not picked up.

I have implemented this in Java to double check that it is not a browser issues, and the Java implementation works fine...so this appears to be something specific to the way Kotlin is interpreting the OnWebSocketClose annotation.

My code looks like the following

import spark.Spark.*
import org.eclipse.jetty.websocket.api.Session
import org.eclipse.jetty.websocket.api.annotations.*;

fun main(args: Array<String>) {
    staticFileLocation("/public")
    webSocket("/chat", WSHandler::class.java)
    init()
}

@WebSocket
class WSHandler {

    @OnWebSocketConnect
    fun connected(session: Session) = println("session connected")

    @OnWebSocketClose
    fun closed(session: Session, statusCode: Int, reason: String) = println("closed sessions")

    @OnWebSocketMessage
    fun message(session: Session, message: String) = println("Got: $message")
}

The html / javascript etc are as per the tutorial on the SparkJava website.


原文:https://stackoverflow.com/questions/44662272
更新时间:2022-02-27 20:02

最满意答案

链接到引用显示两个重载。 编译器选择第一个(树参数重载),因为您将UDTSOCKET作为第一个参数传递具有第二个参数。

由于三参数重载中的第二个参数不是SOCKET ,因此会出现第一个错误。 因为你只将两个参数传递给一个期望三个的函数,所以你会得到第二个错误。

如果要绑定到特定地址(比如传递给函数的sin参数),则需要将指向该地址结构的指针作为第二个参数传递,并将该结构的大小作为第三个参数传递:

UDT::bind(u, (const SOCKADDR*) &sin, sizeof(sin));

如果要使用第二个重载,请使用它:

UDT::bind(sock);

你不能随意混搭,你必须选择一个或另一个。


The linked to reference shows two overloads. The compiler picks the first one (the tree argument overload) because you pass an UDTSOCKET as the first argument and have a second argument.

Since the the second argument in the three-argument overload is not a SOCKET you get the first error. And since you're only passing two arguments to a function expecting three you get the second error.

If you want to bind to a specific address (say the sin argument you pass to your function) then you need to pass a pointer to that address structure as the second argument, and the size of that structure as the third argument:

UDT::bind(u, (const SOCKADDR*) &sin, sizeof(sin));

If you want to use the second overload, then use it:

UDT::bind(sock);

You can't mix and match as you like, you have to pick one or the other.

相关问答

更多
  • 不好意思,但在我看来你错了。 请记住,“SELECT不会被破坏”,微软不会因为转换问题而强制宣传其引擎的关键部分。 您引用的表格来自MSDN中的 十进制和数字存储,它们基本上比int更多 如果您使用严格的别名,那么使用特殊的数据类型会强烈暗示基于整型的类型需要四个字节,而不再是更多。 如果你使用的是CLR类型,那么会有龙,或者更多的开销。 无论如何,您可以通过查看sys.types来验证数据类型的占用空间 Excuse me, but it seems to me you've got it wrong. ...
  • 1.4.0版本中不存在此功能。 我在https://github.com/datastax/spark-cassandra-connector/pull/856上有一个补丁。这应该在将来的版本中修复。 如果你有时间,请测试并评论https://datastax-oss.atlassian.net/browse/SPARKC-271 。 This functionality was not present in the 1.4.0 release. I have a patch at https://gith ...
  • 链接到引用显示两个重载。 编译器选择第一个(树参数重载),因为您将UDTSOCKET作为第一个参数传递并具有第二个参数。 由于三参数重载中的第二个参数不是SOCKET ,因此会出现第一个错误。 因为你只将两个参数传递给一个期望三个的函数,所以你会得到第二个错误。 如果要绑定到特定地址(比如传递给函数的sin参数),则需要将指向该地址结构的指针作为第二个参数传递,并将该结构的大小作为第三个参数传递: UDT::bind(u, (const SOCKADDR*) &sin, sizeof(sin)); 如果要 ...
  • Netty中没有阻止UDT传输。 如果有充分的理由添加旧的I / O UDT传输,请告诉我们。 There is no blocking UDT transport in Netty. If there is a good reason to add the old-I/O UDT transport, please let us know.
  • 在VB6中,用户定义的类型是“值类型”,而类是“引用类型”。 值类型通常存储在堆栈中(除非它们是类的成员)。 引用类型作为指针存储在堆栈中,指向存储实际实例数据的堆中的某个位置。 这意味着对类的引用可以是Nothing (指针为零),而值类型不能。 有多种方法可以解决您的问题: 将Boolean成员添加到您的用户定义类型以指示成功或失败。 创建另一个“包装”UDT(如下所示)并从您的函数中返回。 把你的UDT变成一个类,它可以是Nothing (正如tcarvin所说的 )。 编写函数以返回Boolean并 ...
  • 我终于找到了自己的解决方案,以便接受我自己的答案(无耻但正确的插件),并辅以代码示例。 我原本想重写一个传出数据包的源地址,而不是再次创建套接字已经绑定的套接字。 在这种情况下, 多次调用绑定失败,并且(在我的特殊情况下),我无法为每个源ip分配独立的套接字并使用它。 我在IP_PACKET_INFO中发现了一些引用,但让它正常工作是一件痛苦的事情。 以下参考是有帮助的。 设置udp套接字的来源 示例代码 这是一个简单的应用程序,它创建一个udp套接字,将其绑定到本地端口,然后在发送特定消息之前,它会附加传 ...
  • [1]我想要注意的关于这个源代码的第一件事不是使用另一个表变量( @TP ),而是缺少事务管理 ,也缺少错误处理 。 至少有两个语句(最后两个:UPDATE和INSERT)存在在语句级别生成异常/错误的风险(例如)。 [2]我认为没有理由再使用一个表变量( @TP ),第一个是参数@Supplier_UDT Supplier_UDT 。 它将创建/增加tempdb争用,并且从开发人员的角度来看将创建另一个依赖项(例如:如果我们要在dbo.Supplier表中更改其中一列的数据类型,那么我们还必须更新此存储@ ...
  • 不幸的是,敲钉器的答案只捕获SOL_TCP级别的套接字选项而不是SOL_SOCKET级别的(如SO_KEEPALIVE)。 一些发行版将一些示例与systemtap一起发布。 其中一个是pfiles.stp,您可以使用它从正在运行的进程的套接字中获取套接字选项。 文件示例: $ ./pfiles.stp `pgrep udevd` 787: udevd Current rlimit: 32 file descriptors 0: S_IFCHR mode:0666 dev:0,15 ino: ...
  • 从标准模块,它没有任何错误。 以下代码没有错误。 Public Type TEST_TYPE Prop1 As String End Type Public Function fTest(ByRef param1 As TEST_TYPE) As String param1.Prop1 = "Hello from function" End Function Public Sub sTest(ByRef param1 As TEST_TYPE) param1.Prop1 = ...
  • 简短的回答是da_test_tbl_bigint.java中的表名错误,它尝试插入test_tbl_int 。 我还不清楚为什么驱动程序没有发现错误,当我弄明白时我会更新我的答案。 The short answer is that the table name is wrong in da_test_tbl_bigint.java, it tries to insert into test_tbl_int. It's not yet clear to me why the driver doesn't ca ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。