错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法'Notify”
我正在尝试使用 godbus 创建一个通知服务器,但我无法正确地将我的服务器对象导出到 dbus,并且 dbus 只能识别我的内省 xml。我按照 https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html 来实现它。我还在 godbus 存储库中使用了 _example/server.go,您可能会在下面提供的服务器代码中注意到。 这是服务器代码:
package main import ( "fmt" "os" "github.com/godbus/dbus/v5" "github.com/godbus/dbus/v5/introspect" ) const xml = ` <node> <interface name="org.freedesktop.notifications"> <method name="notify"> <arg direction="in" type="s"/> <arg direction="in" type="u"/> <arg direction="in" type="s"/> <arg direction="in" type="s"/> <arg direction="in" type="s"/> <arg direction="in" type="as"/> <arg direction="in" type="a{sv}"/> <arg direction="in" type="i"/> <arg direction="out" type="u"/> </method> <method name="getcapabilities"> <arg direction="out" type="as"/> </method> <method name="getserverinformation"> <arg direction="out" type="s"/> <arg direction="out" type="s"/> <arg direction="out" type="s"/> <arg direction="out" type="s"/> </method> <method name="closenotification"> <arg direction="in" type="u"/> </method> <signal name="notificationclosed"> <arg type="u" name="id"/> <arg type="u" name="reason"/> </signal> </interface>` + introspect.introspectdatastring + `</node> ` type notificationserver struct { } func (s *notificationserver) notify(appname string, replacesid uint32, appicon string, summary string, body string, actions []string, hints map[string]dbus.variant, expiretimeout int32) (uint32, *dbus.error) { fmt.printf("new notification: %s\n", body) return 0, nil } func (s *notificationserver) getcapabilities() ([]string, *dbus.error) { return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil } func (s *notificationserver) getserverinformation() (string, string, string, string, *dbus.error) { return "antarctica", "antarctica.com", "1.0", "1.2", nil } func (s *notificationserver) closenotification(id uint32) *dbus.error { s.notificationclosed(id, 0) return nil } func (s *notificationserver) notificationclosed(id, reason uint32) { } func main() { conn, err := dbus.connectsessionbus() if err != nil { panic(err) } defer conn.close() reply, err := conn.requestname("com.antarctica.notification", dbus.nameflagdonotqueue) if err != nil { panic(err) } if reply != dbus.requestnamereplyprimaryowner { fmt.fprintln(os.stderr, "name already taken") os.exit(1) } server := notificationserver{} err = conn.export(server,"/org/freedesktop/notifications","org.freedesktop.notifications") if err != nil { panic(err) } conn.export(introspect.introspectable(xml), "/org/freedesktop/notifications", "org.freedesktop.dbus.introspectable") fmt.println("listening on com.antarctica.notification / /com/antarctica/notification ...") select {} }
现在的问题是,即使客户端可以访问内省 xml:
$ gdbus introspect --session --dest com.antarctica.notification --object-path /org/freedesktop/notifications --xml > returns xml
我无法使用我在服务器代码中编写的 org.freedesktop.notifications 方法。例如,notify 未知/无效,这对于每种方法都是相同的:
$ dbus-send --session --print-reply=literal --dest=com.antarctica.notification /org/freedesktop/Notifications org.freedesktop.Notifications.Notify > Error org.freedesktop.DBus.Error.UnknownMethod: Unknown / invalid method 'Notify'
也在 qdbusviewer 中,当我尝试执行任何方法时,它显示“无法在接口 org.freedesktop.notifications 中的路径 /org/freedesktop/notifications 上找到方法 x”
我尝试过的:
- 检查 dbus 是否正在运行
- 检查我的服务器是否正在运行
- 我也尝试重新启动 dbus 服务和我的计算机
- 我认为notificationserver实例(服务器)根本没有被导出,但我不知道为什么
正确答案
这有效。你犯了两个错误:
- com.antarctica.notification
- func (s *notificationserver)
您必须请求“org.freedesktop.notifications”作为名称,并且不能在函数中使用指针。
- org.freedesktop.notifications
- func(通知服务器)
- (你也不需要内省)
package main import ( "fmt" "os" "github.com/godbus/dbus/v5" ) type notificationServer struct{} func (s notificationServer) Notify(appName string, replacesID uint32, appIcon string, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) { fmt.Printf("New notification: %s\n", body) return 0, nil } func (s notificationServer) GetCapabilities() ([]string, *dbus.Error) { return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil } func (s notificationServer) GetServerInformation() (string, string, string, string, *dbus.Error) { return "antarctica", "antarctica.com", "1.0", "1.2", nil } func main() { conn, err := dbus.ConnectSessionBus() if err != nil { panic(err) } defer conn.Close() f := notificationServer{} conn.Export(f, "/org/freedesktop/Notifications", "org.freedesktop.Notifications") reply, err := conn.RequestName("org.freedesktop.Notifications", dbus.NameFlagDoNotQueue) if err != nil { panic(err) } if reply != dbus.RequestNameReplyPrimaryOwner { fmt.Fprintln(os.Stderr, "name already taken") os.Exit(1) } fmt.Println("Listening...") select {} }
以上是错误 org.freedesktop.DBus.Error.UnknownMethod:未知/无效方法'Notify”的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文解释了GO的软件包导入机制:命名imports(例如导入“ fmt”)和空白导入(例如导入_ fmt; fmt;)。 命名导入使包装内容可访问,而空白导入仅执行t

本文解释了Beego的NewFlash()函数,用于Web应用程序中的页间数据传输。 它专注于使用newflash()在控制器之间显示临时消息(成功,错误,警告),并利用会话机制。 Lima

本文详细介绍了MySQL查询结果的有效转换为GO结构切片。 它强调使用数据库/SQL的扫描方法来最佳性能,避免手动解析。 使用DB标签和Robus的结构现场映射的最佳实践

本文演示了创建模拟和存根进行单元测试。 它强调使用接口,提供模拟实现的示例,并讨论最佳实践,例如保持模拟集中并使用断言库。 文章

本文探讨了GO的仿制药自定义类型约束。 它详细介绍了界面如何定义通用功能的最低类型要求,从而改善了类型的安全性和代码可重复使用性。 本文还讨论了局限性和最佳实践

本文详细介绍了在GO中详细介绍有效的文件,将OS.WriteFile(适用于小文件)与OS.openfile和缓冲写入(最佳大型文件)进行比较。 它强调了使用延迟并检查特定错误的可靠错误处理。

本文使用跟踪工具探讨了GO应用程序执行流。 它讨论了手册和自动仪器技术,比较诸如Jaeger,Zipkin和Opentelemetry之类的工具,并突出显示有效的数据可视化
