播放Websocket连接已关闭

时间:2015-11-25 20:47:22

标签: javascript angularjs playframework websocket playframework-2.4

我开始使用webSockets,angular&玩。我使用https://github.com/AngularClass/angular-websocket进行了很好的角度积分。这样的连接:

var dataStream = $websocket('wss://echo.websocket.org/');

效果很好。但是尝试连接到:

var dataStream = $websocket('wss://localhost:9000/socket');

结果:

WebSocket connection to 'wss://localhost:9000/socket' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

在play / netty日志中我只找到:

 play.core.server.netty.PlayDefaultUpstreamHandler - Exception caught in Netty
java.lang.IllegalArgumentException: empty text 

这是我在后端使用的代码:

def socket = WebSocket.acceptWithActor[String, String] { request => out =>
    MyWebSocketActor.props(out)
  }
object MyWebSocketActor {
  def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}
class MyWebSocketActor(out: ActorRef) extends Actor {
  def receive = {
    case msg: String =>
      out ! ("I received your message: " + msg)
  }
}

(基于:https://www.playframework.com/documentation/2.4.x/ScalaWebSockets

这是我的代码: https://github.com/dataplayground/playground/blob/master/public/javascripts/main.js

1 个答案:

答案 0 :(得分:1)

正如我的评论所述:

  1. 您是否尝试过使用普通的ws协议而不是安全协议(wss)?它应该是这样的:public class CalendarActivity extends AppCompatActivity implements WeekView.MonthChangeListener { WeekView mWeekView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calendar); mWeekView = (WeekView) findViewById(R.id.weekView); mWeekView.setMonthChangeListener(this); } @Override public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) { List<WeekViewEvent> events = new ArrayList<WeekViewEvent>(); return events; } } <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/month" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:paddingTop="1dip" android:background="@null"> <com.alamkanak.weekview.WeekView android:id="@+id/weekView" android:layout_width="match_parent" android:layout_height="match_parent" app:eventTextColor="@android:color/white" app:textSize="8sp" app:hourHeight="60dp" app:headerColumnPadding="8dp" app:headerColumnTextColor="#8f000000" app:headerRowPadding="20dp" app:columnGap="0dp" app:noOfVisibleDays="6" app:headerRowBackgroundColor="#ffefefef" app:dayBackgroundColor="#05000000" app:todayBackgroundColor="#05000000" app:headerColumnBackground="#ffffffff"/> </RelativeLayout>

  2. 您已经定义了一个处理$websocket('ws://localhost:9000/socket')的actor,但您实际上是在Angular部分使用JSON。两者都应该匹配,所以请尝试:[String, String]

  3. 我不确定您是否正在返回正确的JSON数据,以及Angular方面的处理。

    首先,如果您使用反斜杠WebSocket.acceptWithActor[JsValue, JsValue]方法(https://www.playframework.com/documentation/2.3.x/api/scala/index.html#play.api.libs.json.JsValue),则会获得\,但这并不一定代表有效的JSON。如果您有JsValue并搜索 ,那么您将获得&#34; test&#34;但这不是一个真正有效的JSON。

    只是为了获得快速反馈:尝试将Websocket Actor更改为{"foo": "test"}类型或者您可以在Actor中构建一个新的JsValue:

    [JsValue, String]

    修改

    这是否来自ng-websocket,您必须获得class MyWebSocketActor(out: ActorRef) extends Actor { def receive = { case msg: JsValue => Logger.debug("actor something " + out) Logger.debug("message " + Json.prettyPrint(msg)) val json: JsValue = Json.parse(""" { "fooReply" : "Something" } """) out ! json } } 属性,而不是直接data

    message