在 Scala 中调用播放 WebSocket 中的方法 [英] Calling a method in play WebSocket in Scala

查看:22
本文介绍了在 Scala 中调用播放 WebSocket 中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala、Play 框架和 Akka 的新手.我的函数定义为

def socket = WebSocket.accept[String, String] { request =>ActorFlow.actorRef(out => MyWebSocketActor.props(out))}

这个我想从其他班级调用.我不清楚如何调用这个函数,因为我不能调用这个

objectName.socket(隐式 req:RequestHeader)

我在 Play 2.5.3、Scala 2.11.7 和 Akka 2.4.7 中工作.

它给了我错误:'=>'预期,')' 找到

解决方案

我仍然不确定这样做有什么好处,但我会尽力回答您的问题.

首先,这个 (objectName.socket(implict req:RequestHeader)) 不是您调用带有隐式参数的方法的方式(您在隐式中也有错别字).

但是正如您已经正确指出的那样,您需要一个隐式的 RequestHeader,因此您只能在 Controller 的上下文中调用此方法.

def anotherControllerAction = objectName.socket

这基本上只是将 anotherControllerAction 指向套接字实现.然后你仍然需要将 anotherControllerAction 放入你的路由文件中.

您可能想要更详细地描述您真正想要实现的目标

I am new to scala, Play framework and Akka. I have function defined as

def socket = WebSocket.accept[String, String] { request =>
    ActorFlow.actorRef(out => MyWebSocketActor.props(out))
  }

This i want to call from other class .I am not clear how to call this function as, i can't call this by

objectName.socket(implict req:RequestHeader)

I am working in Play 2.5.3 , Scala 2.11.7 and Akka 2.4.7 .

Edit: it gives me error : '=>' expected, ')' found

解决方案

I'm still not sure, what the benefit of that would be, but I'll try to answer your question.

First of all, this (objectName.socket(implict req:RequestHeader)) ist not how you call a method with an implicit parameter (also you have a typo there in implicit).

But as you already pointed out correctly, you need an implicit RequestHeader, so you only can call this method within the context of a Controller.

def anotherControllerAction = objectName.socket

That would basically just point anotherControllerAction to the socket implementation. Then you still need to put anotherControllerAction into your routes file.

You might want to describe in more detail, what you actually want to achieve

这篇关于在 Scala 中调用播放 WebSocket 中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆